The Algorithm Design Manual Solution Wiki algorist_users https://algorist.com//algowiki/index.php/Main_Page MediaWiki 1.34.2 first-letter Media Special Talk User User talk The Algorithm Design Manual Solution Wiki The Algorithm Design Manual Solution Wiki talk File File talk MediaWiki MediaWiki talk Template Template talk Help Help talk Category Category talk Gadget Gadget talk Gadget definition Gadget definition talk Main Page 0 1 1 2020-08-18T01:36:51Z MediaWiki default 2 wikitext text/x-wiki <strong>MediaWiki has been installed.</strong> Consult the [https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Contents User's Guide] for information on using the wiki software. == Getting started == * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ] * [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Combating_spam Learn how to combat spam on your wiki] 5702e4d5fd9173246331a889294caf01a3ad3706 2 1 2020-08-23T18:36:40Z Algowikiadmin 1 wikitext text/x-wiki [[About]] [[Problem Solutions]] == Getting started == * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ] * [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Combating_spam Learn how to combat spam on your wiki] 0935494ffc029cef62165486da7cc183da89c3d4 11 2 2020-08-23T19:30:36Z Algowikiadmin 1 wikitext text/x-wiki [[About]] [[Chapter List]] == Getting started == * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ] * [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Combating_spam Learn how to combat spam on your wiki] e1c85d8337e22503547e0f33819c359b41499603 Chapter List 0 2 3 2020-08-23T18:38:14Z Algowikiadmin 1 Created page with "Chapters *[[Chapter 1]] *[[Chapter 2]] *[[Chapter 3]] *[[Chapter 4]] *[[Chapter 5]] *[[Chapter 6]] *[[Chapter 7]] *[[Chapter 8]] *[[Chapter 9]] *[[Chapter 10]] *C..." wikitext text/x-wiki Chapters *[[Chapter 1]] *[[Chapter 2]] *[[Chapter 3]] *[[Chapter 4]] *[[Chapter 5]] *[[Chapter 6]] *[[Chapter 7]] *[[Chapter 8]] *[[Chapter 9]] *[[Chapter 10]] *[[Chapter 11]] *[[Chapter 12]] 36c5723e224215a29c7decea71c656a5b13c3b4f 9 3 2020-08-23T19:30:09Z Algowikiadmin 1 Algowikiadmin moved page [[Problem Solutions]] to [[Chapter List]] wikitext text/x-wiki Chapters *[[Chapter 1]] *[[Chapter 2]] *[[Chapter 3]] *[[Chapter 4]] *[[Chapter 5]] *[[Chapter 6]] *[[Chapter 7]] *[[Chapter 8]] *[[Chapter 9]] *[[Chapter 10]] *[[Chapter 11]] *[[Chapter 12]] 36c5723e224215a29c7decea71c656a5b13c3b4f Chapter 1 0 3 4 2020-08-23T18:42:07Z Algowikiadmin 1 Created page with "Problems *[[1.1]] *[[1.2]] *[[1.3]] *[[1.4]] *[[1.5]] *[[1.6]] *[[1.7]] *[[1.8]] *[[1.9]] *[[1.10]] *[[1.11]] *[[1.12]] *[[1.13]] *[[1.14]] *[[1.15]] *[[1.16]]..." wikitext text/x-wiki Problems *[[1.1]] *[[1.2]] *[[1.3]] *[[1.4]] *[[1.5]] *[[1.6]] *[[1.7]] *[[1.8]] *[[1.9]] *[[1.10]] *[[1.11]] *[[1.12]] *[[1.13]] *[[1.14]] *[[1.15]] *[[1.16]] *[[1.17]] *[[1.18]] *[[1.19]] *[[1.20]] *[[1.21]] *[[1.22]] *[[1.23]] *[[1.24]] *[[1.25]] *[[1.26]] *[[1.27]] *[[1.28]] *[[1.29]] *[[1.30]] *[[1.31]] *[[1.32]] *[[1.33]] *[[1.34]] *[[1.35]] *[[1.36]] *[[1.37]] *[[1.38]] bba027dd1429ac8dd5c583b50ea89483e10c6c1e 5 4 2020-08-23T18:43:03Z Algowikiadmin 1 wikitext text/x-wiki Problems *[[1.1]] *[[1.2]] *[[1.3]] *[[1.4]] *[[1.5]] *[[1.6]] *[[1.7]] *[[1.8]] *[[1.9]] *[[1.10]] *[[1.11]] *[[1.12]] *[[1.13]] *[[1.14]] *[[1.15]] *[[1.16]] *[[1.17]] *[[1.18]] *[[1.19]] *[[1.20]] *[[1.21]] *[[1.22]] *[[1.23]] *[[1.24]] *[[1.25]] *[[1.26]] *[[1.27]] *[[1.28]] *[[1.29]] *[[1.30]] *[[1.31]] *[[1.32]] *[[1.33]] *[[1.34]] *[[1.35]] *[[1.36]] *[[1.37]] *[[1.38]] Back to [[Problem Solutions]] de70c85fbf1d0ba3772b50c6c4c76a21e1d1f92f 7 5 2020-08-23T19:20:07Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn}''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6 :[[1.7]] :1.8 :[[1.9]] :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Problem Solutions]] d33e2b452b5749a50f7cad07206c7c4ca3b0f7b1 8 7 2020-08-23T19:27:53Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]] :1.8 :[[1.9]] :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Problem Solutions]] a10274128dd8a70d0f30990c40fb269a2e137c40 12 8 2020-08-23T19:31:01Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]] :1.8 :[[1.9]] :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] 4e5600b8c18e09e013e9e75bf597ebd8c98fa4ce 25 12 2020-08-23T19:47:52Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8 :[[1.9]] :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] f14b0dc2e290933f1e6d85cda1ea45e3b8632a65 26 25 2020-08-23T19:54:19Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants c ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::'p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] 37c4278ffacdd583ccd75ebb8db9c784ccf67bb7 27 26 2020-08-23T19:55:58Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants c ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::'p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] 51e321b3f2e17e63b7c77918f250dbd74f1daf57 28 27 2020-08-23T20:00:49Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants c ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] 5e34d67b190d84d6beab6105aad924282d06ca3b 29 28 2020-08-23T20:01:27Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] f173114219d575797d94e94234175e12fe81c9af 30 29 2020-08-23T20:03:09Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] b896b443a84cabd298e6b2bfe7317608f8d0c5d7 31 30 2020-08-23T20:07:33Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]].The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>. For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] c80e2985a8cabc1b0d5eb7a7e56cedf9704638c3 32 31 2020-08-23T20:08:49Z Algowikiadmin 1 Undo revision 31 by [[Special:Contributions/Algowikiadmin|Algowikiadmin]] ([[User talk:Algowikiadmin|talk]]) wikitext text/x-wiki Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] b896b443a84cabd298e6b2bfe7317608f8d0c5d7 33 32 2020-08-23T20:11:00Z Algowikiadmin 1 wikitext text/x-wiki =Introduction to Algorithms= Problems :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] a7335f6807016aef3dd16f470f23f20c209cfb20 34 33 2020-08-23T20:13:00Z Algowikiadmin 1 wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10 :[[1.11]] :1.12 :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 :[[1.21]] :1.22 :[[1.23]] :1.24 :[[1.25]] :1.26 :[[1.27]] :1.28 :[[1.29]] :1.30 :[[1.31]] :1.32 :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] 6730d3a63076577cc10cafeb3083f58866e1c6f2 35 34 2020-08-23T20:27:05Z Algowikiadmin 1 wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30 :[[1.31]] :1.32 ===Interview Problems=== :[[1.33]] :1.34 :[[1.35]] :1.36 :[[1.37]] :1.38 Back to [[Chapter List]] 03e35ea0f3efcd6c5af97157471fd3736b6e35f6 36 35 2020-08-23T20:30:22Z Algowikiadmin 1 wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 4c4129cfb1c19aab04fa1d5d3b1072b3641e0a56 39 36 2020-08-24T16:16:00Z Algowikiadmin 1 wikitext text/x-wiki =Introduction to Algorithms= (find a way to do subscripts) ===Finding Counter Examples=== :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] f3d368da19cad4f5a7caf419f6c5867c977c5893 40 39 2020-08-31T16:47:03Z Algowikiadmin 1 /* Introduction to Algorithms */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that ''a'' + ''b'' can be less than ''min(a, b)''. :1.2. Show that ''a'' × ''b'' can be less than ''min(a, b)''. :[[1.3]]. Design/draw a road network with two points ''a'' and ''b'' such that the fastest route between ''a'' and ''b'' is not the shortest route. :1.4. Design/draw a road network with two points ''a'' and ''b'' such that the shortest route between ''a'' and ''b'' is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers ''S'' = {''s1, s2. . . , sn''}, and a target number ''T'', find a subset of ''S'' that adds up exactly to ''T''. For example, there exists a subset within ''S'' = {1, 2, 5, 9, 10} that adds up to ''T'' = 22 but not ''T'' = 23. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an ''S'' and ''T'' where the algorithm does not find a solution that leaves the knapsack completely full, even though a full-knapsack solution exists. ::(a) Put the elements of ''S'' in the knapsack in left to right order if they fit, that is, the first-fit algorithm. ::(b) Put the elements of ''S'' in the knapsack from smallest to largest, that is, the best-fit algorithm. ::(c) Put the elements of ''S'' in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 4c4129cfb1c19aab04fa1d5d3b1072b3641e0a56 41 40 2020-08-31T16:52:44Z Algowikiadmin 1 /* Introduction to Algorithms */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers '<math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 33e2c5fabf71848d777d36dbf3b981046faf10b2 42 41 2020-08-31T16:54:35Z Algowikiadmin 1 /* Finding Counter Examples */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers '<math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets ''S1, . . . , Sm'' of the universal set ''U'' = {1, ..., ''n''}, find the smallest subset of subsets ''T ⊆ S'' such that ''∪ti∈T ti'' = ''U''. For example, consider the subsets ''S1'' = {1, 3, 5}, ''S2'' = {2, 4}, ''S3'' = {1, 4}, and ''S4'' = {2, 5}. The set cover of {1, . . . , 5} would then be ''S1'' and ''S2''. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 39d8f7b4220f44daae7b8eff8b03fae8bf348efc 43 42 2020-08-31T16:57:13Z Algowikiadmin 1 /* Finding Counter Examples */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers '<math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph ''G'' = (''V'', ''E'') asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 04ebae12a5cf41fbc38c9564cba846eb74c8db26 49 43 2020-08-31T20:18:08Z Algowikiadmin 1 /* Introduction to Algorithms */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set ''S'' of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G'' = (''V'', ''E'')</math> asks for the largest subset ''C'' of vertices ''V'' such that there is an edge in ''E'' between every pair of vertices in ''C''. Find a counterexample for the following algorithm: Sort the vertices of ''G'' from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] e5ce337eea9d31a6d3d1bcd47564c4a201ce804c 50 49 2020-08-31T20:19:46Z Algowikiadmin 1 /* Finding Counter Examples */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants ''c'' ≥ 2. ::Multiply(''y, z'') :::''if z'' = 0 ''then'' return(0) ''else'' :::return(Multiply(''cy'', [''z/c'']) + ''y'' · (''z'' mod ''c'')) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial ''anxn + an−1xn−1 + · · · + a1x + a0''. ::Horner(''a, x'') :::''p'' = ''an'' :::for ''i'' from ''n'' − 1 to 0 ::::''p'' = ''p · x'' + ''ai'' :::return ''p'' :1.10. Prove the correctness of the following sorting algorithm. ::Bubblesort (''A'') :::for ''i'' from ''n'' to 1 ::::for ''j'' from 1 to ''i'' − 1 :::::if (''A''[''j''] > ''A''[''j'' + 1]) ::::::swap the values of ''A''[''j''] and ''A''[''j'' + 1] :[[1.11]]. The ''greatest common divisor of positive'' integers ''x'' and ''y'' is the largest integer ''d'' such that ''d'' divides ''x'' and ''d'' divides ''y''. Euclid’s algorithm to compute gcd(''x, y'') where ''x'' > ''y'' reduces the task to a smaller problem: :::::gcd(''x, y'') = gcd(''y, x'' mod ''y'') :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 81b6821264b25b8f38b09d9b3cac7cc1a336633f Chapter 2 0 4 6 2020-08-23T18:49:26Z Algowikiadmin 1 Created page with "Problems *[[2.1]] *2.2 *[[2.3]] *2.4 *[[2.5]] *2.6 *2.7 *2.8 *2.9 *2.10 *2.11 *2.12 *2.13 *2.14 *2.15 *2.16 *2.17 *2.18 *2.19 *2.20 *2.21 *2.22 *2.23 *..." wikitext text/x-wiki Problems *[[2.1]] *2.2 *[[2.3]] *2.4 *[[2.5]] *2.6 *2.7 *2.8 *2.9 *2.10 *2.11 *2.12 *2.13 *2.14 *2.15 *2.16 *2.17 *2.18 *2.19 *2.20 *2.21 *2.22 *2.23 *2.24 *2.25 *2.26 *2.27 *2.28 *2.29 *2.30 *2.31 *2.32 *2.33 *2.34 *2.35 *2.36 *2.37 *2.38 *2.39 *2.40 *2.41 *2.42 *2.43 *2.44 *2.45 *2.46 *2.47 *2.48 *2.49 *2.50 *2.51 *2.52 *2.53 *2.54 *2.55 Back to [[Problem Solutions]] 66527e677bdfd490665a73553ffa61f54e601e46 13 6 2020-08-23T19:32:56Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[2.1]] :2.2 :[[2.3]] "2.4 :[[2.5]] :2.6 :2.7 :2.8 :2.9 :2.10 :2.11 :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 :2.44 :2.45 :2.46 :2.47 :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] b9e882ab08bc514779dab6b678785e993bb90a67 14 13 2020-08-23T19:33:11Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[2.1]] :2.2 :[[2.3]] :2.4 :[[2.5]] :2.6 :2.7 :2.8 :2.9 :2.10 :2.11 :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 :2.44 :2.45 :2.46 :2.47 :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] aa4f84578a8a1ab45fe39ef9060a90babfa13cad Problem Solutions 0 5 10 2020-08-23T19:30:09Z Algowikiadmin 1 Algowikiadmin moved page [[Problem Solutions]] to [[Chapter List]] wikitext text/x-wiki #REDIRECT [[Chapter List]] 708cb94657fd8fa99ddd2dbd00e488dd8190c5fd Chapter 3 0 6 15 2020-08-23T19:33:57Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 4 0 7 16 2020-08-23T19:34:32Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 5 0 8 17 2020-08-23T19:34:48Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 6 0 9 18 2020-08-23T19:35:03Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 7 0 10 19 2020-08-23T19:35:13Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 8 0 11 20 2020-08-23T19:35:22Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 9 0 12 21 2020-08-23T19:35:32Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 10 0 13 22 2020-08-23T19:35:47Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 11 0 14 23 2020-08-23T19:36:02Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 Chapter 12 0 15 24 2020-08-23T19:36:14Z Algowikiadmin 1 Created page with "Problems Back to [[Chapter List]]" wikitext text/x-wiki Problems Back to [[Chapter List]] 4e487692e410b3226e377a6223cc8783377fb746 1.1 0 16 37 2020-08-23T20:35:11Z Algowikiadmin 1 Created page with "''a'' + ''b'' < min(''a,b'') <-> ''a'' < 0 /\ ''b'' < 0 ---- If both ''a'' and ''b'' are negative, ''a'' + ''b''< min(''a, b''). For example ''a'' = -5 ''b'' =..." wikitext text/x-wiki ''a'' + ''b'' < min(''a,b'') <-> ''a'' < 0 /\ ''b'' < 0 ---- If both ''a'' and ''b'' are negative, ''a'' + ''b''< min(''a, b''). For example ''a'' = -5 ''b'' = -7 ''a'' + ''b'' = -5 + (-7) = -12 min(-5, -7) = -7 Back to [[Chapter 1]] 35efef03cce2eb74774dc136443f357d54badf7f 38 37 2020-08-24T16:15:11Z Algowikiadmin 1 wikitext text/x-wiki ''a'' + ''b'' < min(''a,b'') <-> ''a'' < 0 /\ ''b'' < 0 (find way to fix math symbols) ---- If both ''a'' and ''b'' are negative, ''a'' + ''b''< min(''a, b''). For example ''a'' = -5 ''b'' = -7 ''a'' + ''b'' = -5 + (-7) = -12 min(-5, -7) = -7 Back to [[Chapter 1]] 93d6d469c6e105574ca7ce72f6ab1d9b0f45a2e8 44 38 2020-08-31T20:07:11Z Algowikiadmin 1 wikitext text/x-wiki <math>a + b < \min(a,b) \Leftrightarrow a < 0 \and b < 0</math> ---- If both ''a'' and ''b'' are negative, <math>a + b < min(a, b)</math>. For example <math>a = -5</math> <math>b = -7</math> <math>a+ b = -5 + (-7) = -12</math> <math>min(-5, -7) = -7</math> Back to [[Chapter 1]] 623eb12327478ef0cf6af2e7b13c8ede3692807c 1.3 0 17 45 2020-08-31T20:09:25Z Algowikiadmin 1 Created page with " a ----------- c ----------- b \ / \--------- d ---------- / If the distance from ''a'' to ''b'' going through ''d'' is less than the dista..." wikitext text/x-wiki a ----------- c ----------- b \ / \--------- d ---------- / If the distance from ''a'' to ''b'' going through ''d'' is less than the distance from ''a'' to ''b'' going through ''c'' but there is a busy traffic intersection at ''d'' with a stop sign that is always backed up, then the route from ''a'' to ''b'' through ''c'' is faster, but the route through ''d'' is shorter. For example, <math>dist(a, c) = 10</math> miles <math>dist(c, b) = 5</math> miles So the distance from ''a'' to ''b'' through ''c'' is 15 miles. Assuming you drive 30 miles per hour, the time to travel this would be 30 minutes <math>dist(a, d) = 5</math> miles <math>dist(c, b) = 5</math> miles So the distance from ''a'' to ''b'' through ''d'' is 10 miles. Assuming you drive 30 miles per hour, the time to travel this would be 20 minutes, but due to the busy intersection at ''d'', you are delayed 15 minutes, the total time would be 35 minutes. --- Another possible solution: You have a longer route with a higher speed and a shorter route with a lower speed. By choosing the numbers appropriately, you can make the longer route be the winner. The speed may be due to law, or due to traffic jam, or because of pot holes on the road :-) Suppose road of length l1 has speed limit s1 and another road of length l2 has speed limit l2. The shorter route is determined by comparing l1 with l2. The faster route is determined by comparing l1/s1 with l2/s2. With positive numbers, if l1 > l2 but l1 < l2 * s1 / s2, then the faster route differs from the shorter route. Back to [[Chapter 1]] 9f5bad0c21178dc179ce00c1b2f3d2c8033545ab 1.5 0 18 46 2020-08-31T20:10:18Z Algowikiadmin 1 Created page with "<p>First-fit algorithm counterexample:</p> <p> <math>S = \{1, 2, 3\}</math> <br/> <math>T = 5</math> </p> <p>Best-fit algorithm counterexample:</p> <p> <math>S = \{1, 2, 3\}<..." wikitext text/x-wiki <p>First-fit algorithm counterexample:</p> <p> <math>S = \{1, 2, 3\}</math> <br/> <math>T = 5</math> </p> <p>Best-fit algorithm counterexample:</p> <p> <math>S = \{1, 2, 3\}</math> <br/> <math>T = 5</math> </p> <p>Largest to smallest algorithm counterexample:</p> <p> <math>S = \{4, 3, 2\}</math> <br/> <math>T = 5</math> </p> ---- <p>Another counterexample:</p> If <math>S = \{1, 2, 5, 9, 10\}</math> and <math>T=11</math>, the solution would be the subset <math>\{2, 9\}</math> or <math>\{1, 10\}</math> which none of the algorithms found. Using first-fit algorithm (left to right) {| class="wikitable" |- ! Item added ! Subset of <math>S</math> ! Sum |- | 1 | <math>\{1\}</math> | 1 < 11 |- | 2 | <math>\{1, 2\}</math> | 3 < 11 |- | 5 | <math>\{1, 2, 5\}</math> | 8 < 11 |- | 9 | <math>\{1, 2, 5, 9\}</math> | 17 > 11 |} Using best-fit algorithm (smallest to largest) {| class="wikitable" |- ! Item added ! Subset of <math>S</math> ! Sum |- | 1 | <math>\{1\}</math> | 1 < 11 |- | 2 | <math>\{1, 2\}</math> | 3 < 11 |- | 5 | <math>\{1, 2, 5\}</math> | 8 < 11 |- | 9 | <math>\{1, 2, 5, 9\}</math> | 17 > 11 |} Using largest to smallest {| class="wikitable" |- ! Item added ! Subset of <math>S</math> ! Sum |- | 10 | <math>\{10\}</math> | 10 < 11 |- | 9 | <math>\{9, 10\}</math> | 19 > 11 |} Back to [[Chapter 1]] d7fc7112144600e849d8045ae5104ccce3ff4011 1.7 0 19 47 2020-08-31T20:11:55Z Algowikiadmin 1 Created page with "Please add proposed solutions to the question here." wikitext text/x-wiki Please add proposed solutions to the question here. 05e58cd5b939f8f36f0908fe64a2854a8f38c83d 48 47 2020-08-31T20:12:13Z Algowikiadmin 1 wikitext text/x-wiki Please add proposed solutions to the question here. Back to [[Chapter 1]] bdeb1f1f01eac53f913fec8c571b77b2ed2f4946 Chapter 1 0 3 51 50 2020-08-31T20:42:52Z Algowikiadmin 1 /* Proofs of Correctness */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. ''function'' multiply(<math>y,z</math>) ''comment'' Return the product <math>yz</math>. 1. ''if'' <math>z=0</math> ''then'' return(0) ''else'' 2. return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> ''function'' horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. ''function'' bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) var int <math>i, j</math> for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute gcd(x, y) where <math>x > y</math> reduces the task to a smaller problem: :::::<math><gcd(x, y) = gcd(y, x mod y)/math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] dd9acd9d31f4a368f263119dc5ad81f7ff3626a1 52 51 2020-09-01T11:57:37Z Algowikiadmin 1 /* Proofs of Correctness */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. 1. ''if'' <math>z=0</math> ''then'' return(0) ''else'' 2. return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute gcd(x, y) where <math>x > y</math> reduces the task to a smaller problem: :::::<math><gcd(x, y) = gcd(y, x mod y)/math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] e81542deb9baf05fd27a7228797ad6a73e765170 53 52 2020-09-01T11:58:49Z Algowikiadmin 1 /* Proofs of Correctness */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute gcd(x, y) where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 8fff96ee8cf9937990c9e6cbab4524098fe76587 54 53 2020-09-01T11:59:57Z Algowikiadmin 1 /* Proofs of Correctness */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. :[[1.13]] :1.14 :[[1.15]] :1.16 :[[1.17]] :1.18 :[[1.19]] :1.20 ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 5a3725643f61a1436d638e87306f0bf7ece94145 62 54 2020-09-01T12:12:18Z Algowikiadmin 1 /* Induction */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math>} :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. <math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 08f40f224ecdc7f622e79b66f58d51fbe0ecff20 63 62 2020-09-01T12:14:42Z Algowikiadmin 1 /* Induction */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] a09c3b4d1616a57ad2c1571bb8e6fe7ea940e5f0 65 63 2020-09-01T12:16:25Z Algowikiadmin 1 /* Proofs of Correctness */ wikitext text/x-wiki =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] a924dddceeaeec6a6f01c4015f0471600e1a0004 74 65 2020-09-01T12:29:05Z Algowikiadmin 1 wikitext text/x-wiki Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number. =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] d5d64f552186910b9ee9caa5e999b42d441b7baa 75 74 2020-09-02T16:10:08Z Algowikiadmin 1 /* Introduction to Algorithms */ wikitext text/x-wiki Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number. =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. ::[[1.1|Solution]] :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. ::[[1.3|Solution]] :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. ::[[1.5|Solution]] :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. ::[[1.7|Solution]] ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> ::[[1.9|Solution]] :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. ::[[1.11|Solution]] ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. ::[[1.13|Solution]] :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> ::[[1.15|Solution]] :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> ::[[1.17|Solution]] :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. ::[[1.19|Solution]] :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? ::[[1.21|Solution]] :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. ::[[1.23|Solution]] :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. ::[[1.25|Solution]] :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? ::[[1.27|Solution]] :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? ::[[1.29|Solution]] ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. ::[[1.31|Solution]] ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. ::[[1.33|Solution]] :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? ::[[1.35|Solution]] :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? ::[[1.37|Solution]] :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 2f5a6b2daa22efe45875d684fb535b6bc1b8f4c8 1.9 0 20 55 2020-09-01T12:01:15Z Algowikiadmin 1 Created blank page wikitext text/x-wiki da39a3ee5e6b4b0d3255bfef95601890afd80709 56 55 2020-09-01T12:01:32Z Algowikiadmin 1 wikitext text/x-wiki Back to [[Chapter 1]]. 37f639deed9d133b36b97dce6f03fb1a1fb6d323 1.11 0 21 57 2020-09-01T12:02:55Z Algowikiadmin 1 Created page with "Back to [[Chapter 1]]." wikitext text/x-wiki Back to [[Chapter 1]]. 37f639deed9d133b36b97dce6f03fb1a1fb6d323 1.21 0 22 58 2020-09-01T12:03:35Z Algowikiadmin 1 Created page with "Back to [[Chapter 1]]." wikitext text/x-wiki Back to [[Chapter 1]]. 37f639deed9d133b36b97dce6f03fb1a1fb6d323 1.23 0 23 59 2020-09-01T12:04:24Z Algowikiadmin 1 Created page with "1 million seconds = 277.777778 hours 1 million seconds = 11.5740741 days ---- Possible approach: a) There are 3600s in an hour b) Eliminating the thousands, we get 1000..." wikitext text/x-wiki 1 million seconds = 277.777778 hours 1 million seconds = 11.5740741 days ---- Possible approach: a) There are 3600s in an hour b) Eliminating the thousands, we get 1000 / 3.6 c) 3.6 is about half-way between 3 and 4, thus the result is about (1000/3 + 1000/4)/2 ≈ (333+250) = 583/2 ≈ 290 d) About 12 days (240 + 48) Another approach: a) There are 3600s in an hour b) 3600s goes into 10,000s ≈ 3 hours c) multiply 3 with 100 = 300 hours (since 10,000s is 100 times less than 1,000,000) d) 24h goes into 30h ≈ 1.2 day e) multiply 1.2 by 10 = 12 days (since 30h is 10 times less than 300) Back to [[Chapter 1]]. 795f649174aa9142dfacf16fccb70eec85fefc33 1.25 0 24 60 2020-09-01T12:06:20Z Algowikiadmin 1 Created page with "I estimate the mouth of the Mississippi at 1 mile wide and 100 feet, or 0.02 miles, deep. If the water were moving at 10 miles an hour, that means that 10 miles x 0.02 miles x..." wikitext text/x-wiki I estimate the mouth of the Mississippi at 1 mile wide and 100 feet, or 0.02 miles, deep. If the water were moving at 10 miles an hour, that means that 10 miles x 0.02 miles x 1 miles flow through the mouth every hour. 10 x 0.02 x 1 = 0.2, times 24 hours/day = 4.8 cubic miles of water/day. One assumption is that the tidal currents of the ocean can be neglected because within each 24-hour-period the tide rises and falls twice thus canceling out itself. Back to [[Chapter 1]]. cae0362808b862a06434b3cab0f2f6f1f09377c6 1.29 0 25 61 2020-09-01T12:07:05Z Algowikiadmin 1 Created page with "1. If there are 10 times as many items, and it is proportional to <math>n^2</math>, it will take <math>10^2</math> times as long or 100 seconds. 2. If it proportional to <math..." wikitext text/x-wiki 1. If there are 10 times as many items, and it is proportional to <math>n^2</math>, it will take <math>10^2</math> times as long or 100 seconds. 2. If it proportional to <math>nlogn</math>, it will take <math>(10^4.log_{2} 10^4) / (10^3.log_{2} 10^3)</math> as long or about 13.33 seconds. Back to [[Chapter 1]]. a69ff9c8eb4a037947f3accbb8b88af0c0868d45 1.27 0 26 64 2020-09-01T12:15:13Z Algowikiadmin 1 Created page with "Return to [[Chapter 1]]." wikitext text/x-wiki Return to [[Chapter 1]]. d8413421a7980babda8c2af3b3483435a232053b 1.13 0 27 66 2020-09-01T12:18:00Z Algowikiadmin 1 Created page with "The basis case is when <math>n = 0</math><br> :<math>\sum_{i=1}^0 i^2 = 0^2 = 0 </math><br> and using <math>n=0</math> in the formula <math>\frac {n(n + 1)(2 \cdot n + 1)} {6}..." wikitext text/x-wiki The basis case is when <math>n = 0</math><br> :<math>\sum_{i=1}^0 i^2 = 0^2 = 0 </math><br> and using <math>n=0</math> in the formula <math>\frac {n(n + 1)(2 \cdot n + 1)} {6}</math> you get:<br> :<math>\frac {0(0 + 1)(2 \cdot 0 + 1)} {6} = \frac {0(1)(0 + 1)} {6} = \frac {0} {6} = 0</math><br> Since these are equal, the basis case is true.<br> Now, I will show that on the assumption that the summation is true for ''n'', it follows that it is true for <math>n+1</math> :<math>\sum_{i = 1}^{n + 1} i^2 = (n + 1)^2 + \sum_{i = 1}^n i^2 = (n^2 + 2n + 1) + \frac{n(n + 1)(2 \cdot n + 1)} {6} = \frac{6n^2 + 12n + 6}{6}+ \frac{2n^3 + 3n^2 + n} {6} = \frac {2n^3 + 9n^2 + 13n + 6} {6}</math><br> Which should be equal to the formula <math>\frac {n(n + 1)(2 \cdot n + 1)} {6}</math> when <math>n = n + 1</math>: <math>\frac {(n + 1)(n + 1 + 1)(2 \cdot (n + 1) + 1)} {6}= \frac {(n + 1)(n + 2)(2n + 2 + 1)} {6}= \frac {(n^2 + 3n + 2)(2n + 3)} {6}= \frac {2n^3 + 9n^2 + 13n + 6} {6}</math> Back to [[Chapter 1]]. c3800dac992787b94393feb1a7fb1ad6984864ac 1.15 0 28 67 2020-09-01T12:18:58Z Algowikiadmin 1 Created page with "Call the statement <math>S_n</math> and the general term <math>a_n</math><br> <b>Step 1:</b> Show that the statement holds for the basis case <math>n = 0</math><br> :<math>a..." wikitext text/x-wiki Call the statement <math>S_n</math> and the general term <math>a_n</math><br> <b>Step 1:</b> Show that the statement holds for the basis case <math>n = 0</math><br> :<math>a_0 = 0 \cdot (0 + 1)(0 + 2) = 0</math> <br><br> :<math>S_0 = \frac {0 \cdot (0 + 1)(0 + 2)(0 + 3)} {4} = \frac {0} {4} = 0 </math><br> Since <math>a_n = S_n</math>, the basis case is true.<br><br> <b>Step 2:</b> Assume that <math>n = k</math> holds. :<math>S_k = \frac {k(k + 1)(k + 2)(k + 3)} {4}</math><br><br> <b>Step 3:</b> Show that on the assumption that the summation is true for ''k'', it follows that it is true for ''k + 1''. :<math> \begin{align} S_{k + 1} & = \sum_{i = 1}^k i(i + 1)(i + 2) + a_{k + 1} \\ & = \frac{k(k + 1)(k + 2)(k + 3)} {4} + (k + 1)((k + 1) + 1)((k + 1) + 2) \\ & = \frac{k(k + 1)(k + 2)(k + 3)} {4} + (k + 1)(k + 2)(k + 3) \\ & = \frac{k(k + 1)(k + 2)(k + 3)}{4} + \frac{4 \cdot (k + 1)(k + 2)(k + 3)} {4} \\ \end{align} </math><br> It's easier to factor than expand. Notice the common factor of ''(k + 1)(k + 2)(k + 3)''. :<math>S_{k + 1} = \frac{(k + 1)(k + 2)(k + 3)(k + 4)}{4}</math><br> This should be equal to the formula <math>S_k = \frac{k(k + 1)(k + 2)(k + 3)}{4}</math> when ''k = k + 1'':<br> :<math>\frac{(k + 1)((k + 1) + 1)((k + 2) + 2)((k + 1) + 3)} {4} = \frac{(k + 1)(k + 2)(k + 3)(k + 4)} {4}</math><br><br> Since both the basis and the inductive step have been proved, it has now been proved by mathematical induction that S_n holds for all natural n. Back to [[Chapter 1]]. b7929b8d79bd236976c23a6a04cc0c309945d9a9 1.17 0 29 68 2020-09-01T12:19:40Z Algowikiadmin 1 Created page with "<b>Step 1:</b> Show that the statement holds for the basis case <math>n = 1</math><br> :<math>\frac {1}{i(i+1)} = \frac {n}{n+1}</math><br><br> :<math>\frac {1}{1(1+1)} = \fr..." wikitext text/x-wiki <b>Step 1:</b> Show that the statement holds for the basis case <math>n = 1</math><br> :<math>\frac {1}{i(i+1)} = \frac {n}{n+1}</math><br><br> :<math>\frac {1}{1(1+1)} = \frac {1}{1+1}</math><br><br> :<math>\frac {1}{2} = \frac {1}{2}</math><br><br> Since <math>1/2 = 1/2</math>, the basis case is true.<br><br> <b>Step 2:</b> Assume that that summation is true up to ''n''.<br><br> <b>Step 3:</b> Show that on the assumption that the summation is true for ''n'', it follows that it is true for ''n + 1''. <math>\sum_{i = 1}^{n+1} = \frac{n+1}{n+1+1} = \frac{n}{n+1} + \frac{1}{(n+1)(n+1+1)}</math><br> <math>\frac{n+1}{n+2} = \frac{n(n+2)}{(n+1)(n+2)} + \frac{1}{(n+1)(n+2)}</math><br> <math>\frac{n+1}{n+2} = \frac{n(n+2)+1}{(n+1)(n+2)}</math><br> <math>\frac{n+1}{n+2} = \frac{n^2+2n+1}{(n+1)(n+2)}</math><br> <math>\frac{n+1}{n+2} = \frac{(n+1)(n+1)}{(n+1)(n+2)}</math><br> <math>\frac{n+1}{n+2} = \frac{(n+1)}{(n+2)}</math><br> QED Back to [[Chapter 1]]. c86e9131e68d7099d1844eb4e9fefdec82aec3a0 1.19 0 30 69 2020-09-01T12:20:19Z Algowikiadmin 1 Created page with "<b>Step 1:</b> Show that the statement holds for the basis case <math>n = 1</math><br> :<math>E(n) = n - 1</math><br> :<math>E(1) = 1 - 1 = 0</math>. A tree with one node has..." wikitext text/x-wiki <b>Step 1:</b> Show that the statement holds for the basis case <math>n = 1</math><br> :<math>E(n) = n - 1</math><br> :<math>E(1) = 1 - 1 = 0</math>. A tree with one node has zero edges <b>Step 2:</b> Assume that that summation is true up to ''n''.<br><br> <b>Step 3:</b> Show that on the assumption that the summation is true for ''n'', it follows that it is true for ''n + 1''. :<math>E\left(n + 1\right) = n + 1 - 1</math><br> :<math>\Leftrightarrow E(n) + 1 = n</math> When adding one node to a tree one edge is added as well<br> :<math>\Leftrightarrow n -1 + 1 = n</math><br> :<math>\Leftrightarrow n = n</math><br> QED Back to [[Chapter 1]]. fee2252982d571bda0089b7a8ed11b63de1f893a 1.31 0 31 70 2020-09-01T12:21:18Z Algowikiadmin 1 Created page with "Back to [[Chapter 1]]." wikitext text/x-wiki Back to [[Chapter 1]]. 37f639deed9d133b36b97dce6f03fb1a1fb6d323 1.33 0 32 71 2020-09-01T12:23:23Z Algowikiadmin 1 Created page with " Answer: Seven races. '''First 5 races:''' Divide 25 horses into 5 groups and that gives you 5 winners. '''Sixth race:''' Now race 5 of them that will give you winner and wh..." wikitext text/x-wiki Answer: Seven races. '''First 5 races:''' Divide 25 horses into 5 groups and that gives you 5 winners. '''Sixth race:''' Now race 5 of them that will give you winner and which two are the bottom-most group. Reject the bottom two group Now consider the first 3 groups, G1, G2 and G3 G3 Group (the horse that came 3rd in 6th race) - can only bid for 3rd place so we take him for next level i.e. G31 G2 Group (the horse that came 2nd in 6th race) - can bid for 2& 3rd spots we take top two from this group for next level i.e G21, G22 G1 Group (the horse that came 1st in 6th race) - can bid for 1,2,3rd place, but we have already got our first; so we need only to take 2 from this group for next level i.e. G11, G12, G13 '''Seventh Race:''': G12, G13, G21, G22, G31 Race them to get 2nd and 3rd winner G11 is already won as 1st winner. --[[User:Max|Max]] 07:06, 16 June 2010 (EDT) I get the same answer but want to clarify the notation and solution approach. Answer: Seven races. '''First five races:''' First 5 exclusive groups of 5 horses. '''Sixth race:''' Between all the first-place winners in first five races (leaders). Letter the groups by the order the leaders in the sixth race (e.g. ''a1'' is the fastest of the leaders while ''e1'' is the slowest ''leader''). Let's list the constraints implied by these results (where ''>>'' means faster). X(n) >> X(n+1), a1 >> b1, b1 >> c1, c1 >> d1, d1 >> e1 These constraints allow us to sketch a tree of continuations for the possible ordering: <pre> a1 | \ a2 b1--c1--d1--e1 | | | | | a3 b2 c2 d2 e2 </pre> Now, we need to devise the next race to give us an answer. Reasoning by constraints and seeking three fastest we can knock out anyone lower than c1 since that would imply that c2 >> b1 >> c1 which is a contradiction. That leaves us with one more race between a2, a3, b1, b2, c1 to determine the second and third fastest horses. '''Argument:''' In the above solution, we are assuming that the winners of each of the groups are better than all the remaining horses in every group. But that might not be true. For example, let us denote the speed (20) of a horse A as A(20). Now let us consider a collection of 9 horses: Group I A(10) B(20) C(30) winner: C(30) Group II D(40) E(50) F(60) winner: F(60) Group III G(70) H(80) I(90) winner: I(90) We clearly see that if we continue with ONLY the winner of the groups, we get an incorrect answer. We need to pick the top 3 of each group to get the correct answer. ---- --[[User:Luckylovelace|Luckylovelace]] ([[User talk:Luckylovelace|talk]]) 17:52, 19 September 2014 (UTC) '''Possible solution to proposed argument:''' I was thinking the same thing as the person who proposed the argument above. However, I think I've reconciled my differences with the first 2 solutions. I'll explain. Initially, my solution was to setup the contest (since it is not specified how this can be done) by racing all 5 groups of horses and then only racing the fastest 5 (not necessarily the winner of each group). If the problem is setup in this way then it would only require 6 races. Even better yet you could just race all 5 groups and take the fastest 3 horses, which would only take 5 races. This was my lazyman, practical approach. After this, I began to think about how you would go about creating a solution if no times were given, meaning you wouldn't know the ordering of the groups. You only know which horse is faster than the other horses in its group. And of course, we'll have to still have at least 5 races so that every horse gets their chance. This will result in 5 groups. So let me clarify my setup of the problem: The 1st place horse must be faster than 24 other horses The 2nd place horse must be faster than 23 other horses (all horses except #1) The 3rd place horse must be faster than 22 other horses (all horses except #2 and #1) After the first race we know this: Group A (''X < Y means x is faster than y''): A1 < A2 < A3 < A4 < A5 (So we know A1 is faster than 4 other horses) ... Group E: E1 < E2 < E3 < E4 < E5 (So we know E1 is faster than 4 other horses) '''So we now have A1, B1, C1, D1, E1 as winners of their group, which are all faster than at least 4 other horses.''' So now in the 6th race, we determine the first place horse because, we know that they must be faster than all 24 other horses since, they are faster than the fastest horses of all the other groups. To clarify: (Assuming they placed in lexical order) A1 < B1 < C1 < D1 < E1 '''=>''' A1 < (B1...B5...C1...C5...D1...D5...E1...E5) However, we cannot determine 2nd or 3rd place, because other horses in the first group may be faster than horses in 2nd or 3rd place, and other horses in the 2nd group, may be faster than horses in 3rd place. But, what we do know, is that the horses in 4th and 5th place are slower than all the remaining horses ( B1 < C1 < D1..E5). So we can remove them from the next race. This opens up 3 new slots (since 1st place has already been determined). Now, as stated previously, we know that 2nd and 3rd could both come from the first group, therefore we must take the next two fastest horses from group 1 (A2, A3). We then know that 3rd place could also come from another horse in the 2nd group, therefore we must take 1 horse from that group (B2). '''This leaves us with the last and final race between A2, A3, B1, B2, C1.''' '''Q: Why not take all 3 slots from the first group?''' This would not make sense since there are only 2 places (2nd and 3rd) left to give. If we did do this, we could potentially be left with this ordering: A1 < B1 < C1 < A2 < A3, which never gives B2 a chance to claim the third place spot should he be faster than C1. Another way to look at it is as two separate races - one for 2nd, and another for 3rd. In the first race you have A2 and B1 vying for 2nd place, and in the second race, you have A3, B2, and C1 racing for 3rd. Since we are trying to minimize the number of races, we can just combine the two into one race of 5 to determine the final outcome. I'm guessing this is why it's an interview question. It's vague enough such that there are multiple solutions, which would allow the interviewers to glean a bit of your thought process. We need '''Seven races''' 1. Divide equally 25 horses into 5 groups: A, B, C, D, E and run 5 races, we will get the following result: <pre> A: A1 > A2 > A3 >.. B: B1 > B2 > B3 >.. C: C1 > C2 > C3 >.. D: D1 > D2 > D3 >.. E: E1 > E2 > E3 >.. </pre> Other horses were eliminated because they have no chance to belong to TOP 3 fastest horses. 2. Run 6th race with (A1, B1, C1, D1, E1), suppose the result is: <pre> A: A1 > A2 > A3 > .. V B: B1 > B2 > B3 > .. V C: C1 > C2 > C3 > .. V D: D1 > D2 > D3 > .. V E: E1 > E2 > E3 > .. </pre> For now, '''A1''' is the fastest horse. After finding out '''A1''': - '''D(x)''', '''E(x)''', '''C2''' and '''C3''' can be eliminated because '''[B1, C1]''' is always the better solution. - '''B3''' can also be eliminated because of '''[B1, B2]'''. <pre> A: A2 > A3 B: B1 > B2 V C: C1 </pre> 3. Run 7th race with remaining horses, we will find out the second fastest and third fastest horse. --[[User:Sandyvu|Sandyvu]] ([[User talk:Sandyvu|talk]]) 11:58, 2 April 2017 (UTC) Consider the following case, where A is a group name and the numbers are the hypothetical speeds of the horses: <pre> A = {91, 92, 93, 94, 95} B = {81, 82, 83, 84, 85} C = {71, 72, 73, 74, 75} D = {61, 62, 63, 64, 65} E = {51, 52, 53, 54, 55} </pre> In this scenario, we need 8 races to determine first, second and third fastest horses. -- [[User:Radupopa21]] Are we over-thinking this? Nothing is stated about presence or absence of stopwatches. Assuming that all races are run over the same distance, run 5 races, with 5 stopwatches timing the 5 competitors. That gives you 25 timings from which you can easily select the top three. — [[User:Peter]] Back to [[Chapter 1]]. c37fd2a5ab5de8c9702de6bf6609ebde06e42541 1.35 0 33 72 2020-09-01T12:25:02Z Algowikiadmin 1 Created page with "'''Assumptions''': : approx 400000 cars : each car needs to refuel once a week : each gas station is open 10 hours a day and refuels 10 cars an hour : there are enough station..." wikitext text/x-wiki '''Assumptions''': : approx 400000 cars : each car needs to refuel once a week : each gas station is open 10 hours a day and refuels 10 cars an hour : there are enough stations to refuel all cars once per week '''Calculation''': : cars that can be fueled by 1 station in 1 week : 10*10*7=700 : a number of gas stations (rounded up): : ceil(400000/700)=572 ---- '''A slightly different approach''': * approx 300 mln cars in the US (1 car per each citizen). * each station is open 12hr a day, has 6 places for taking fuel, each fueling takes about 6 min * amount of cars using given gas station daily: <math>6 * 12 * \frac{60}{6} = 6 * 120 = 720 </math> * gas station (at least in Europe) are used always used, so: <math>\frac{300 000 000}{720} \approx 333 000</math> gas stations. Back to [[Chapter 1]]. f86bc656499af06f95f3a5bee0eb90a8a8237caa 1.37 0 34 73 2020-09-01T12:25:44Z Algowikiadmin 1 Created page with "I'm envisioning the United States as a rectangle 1000 miles high and 3000 miles long. I'm not including Alaska, because, although it's large, it doesn't have many roads. Much..." wikitext text/x-wiki I'm envisioning the United States as a rectangle 1000 miles high and 3000 miles long. I'm not including Alaska, because, although it's large, it doesn't have many roads. Much of the U.S. is rural, so roads are fairly sparse. In the semi-rural area of New York State where I grew up, it was common to go a mile or so without hitting a road. So it's reasonable to imagine the U.S. as a grid of roads, each one mile from the other. That means that if all roads were evenly distributed, the U.S. would be covered with a grid of 1000 roads of 3000 miles each, and 3000 roads of 1000 miles each, for a total of 6,000,000 miles of roads. ''Editor's Note: Not bad, according to [http://www.nationalatlas.gov/transportation.html National Atlas] there are about 4,000,000 miles of roads in the U.S. Back to [[Chapter 1]]. b491e62aa7d38c25205c03047cf03f04ff12009d Chapter 2 0 4 76 14 2020-09-03T17:53:25Z Algowikiadmin 1 wikitext text/x-wiki Problems :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. '''function''' mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2 :[[2.3]] :2.4 :[[2.5]] :2.6 :2.7 :2.8 :2.9 :2.10 :2.11 :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 :2.44 :2.45 :2.46 :2.47 :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] f5d19afc5e56b3c1301b6ec37c489b8f96ffc12d 77 76 2020-09-03T17:57:15Z Algowikiadmin 1 wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. '''function''' mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2 :[[2.3]] :2.4 :[[2.5]] :2.6 :2.7 ===Big Oh=== :2.8 :2.9 :2.10 :2.11 :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] b7babfdb9a9a6b49549c77cf36d854d479d7b6f1 78 77 2020-09-03T18:03:38Z Algowikiadmin 1 /* Program Analysis */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 return(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 return(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> return(<math>r</math>) :[[2.5]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> end #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8 :2.9 :2.10 :2.11 :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 46832e47b6bad6b7b3bec4e4e8f6c160c652da28 79 78 2020-09-03T18:58:06Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 return(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 return(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> return(<math>r</math>) :[[2.5]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> end #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.11|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]] :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 4d76c3ae9e24ceb5ba770d98ac5f2a7f65088c5a 80 79 2020-09-03T19:02:58Z Algowikiadmin 1 /* Algorithm Analysis */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.11|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]] :2.12 :2.13 :2.14 :2.15 :2.16 :2.17 :2.18 :2.19 :2.20 :2.21 :2.22 :2.23 :2.24 :2.25 :2.26 :2.27 :2.28 :2.29 :2.30 :2.31 :2.32 :2.33 :2.34 :2.35 :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 801c7163e04e8d3e224b9bf069811f0b296e27e2 81 80 2020-09-03T19:17:37Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n))</math>, \Theta(g(n)), \Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\THeta(n^2) = \Theta(n^2+1). :[[2.15]] [[2.15|Solution]] :2.16 :[[2.17]] [[2.17|Solution]] :2.18 :[[2.19]] [[2.19|Solution]] :2.20 :[[2.21]] [[2.21|Solution]] :2.22 :[[2.23]] [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] e213f94160e63b2cea5942aecf67de33bab2b4ee 82 81 2020-09-03T19:18:57Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n))</math>,\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)<\math>. :[[2.15]] [[2.15|Solution]] :2.16 :[[2.17]] [[2.17|Solution]] :2.18 :[[2.19]] [[2.19|Solution]] :2.20 :[[2.21]] [[2.21|Solution]] :2.22 :[[2.23]] [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 7f28a4bebcfde57f3e550fc043bb7d11e5f8110e 83 82 2020-09-03T19:22:33Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)<\math>. :[[2.15]] [[2.15|Solution]] :2.16 :[[2.17]] [[2.17|Solution]] :2.18 :[[2.19]] [[2.19|Solution]] :2.20 :[[2.21]] [[2.21|Solution]] :2.22 :[[2.23]] [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 441e214fa0feac85f9d0d68488e0c15f938835a6 84 83 2020-09-03T19:34:05Z Algowikiadmin 1 /* Program Analysis */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)<\math>. :[[2.15]] [[2.15|Solution]] :2.16 :[[2.17]] [[2.17|Solution]] :2.18 :[[2.19]] [[2.19|Solution]] :2.20 :[[2.21]] [[2.21|Solution]] :2.22 :[[2.23]] [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] c9e983de7ce0a6021bd9a11b00a5a389fcba9744 85 84 2020-09-03T19:35:07Z Algowikiadmin 1 /* Algorithm Analysis */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)<\math>. :[[2.15]] [[2.15|Solution]] :2.16 :[[2.17]] [[2.17|Solution]] :2.18 :[[2.19]] [[2.19|Solution]] :2.20 :[[2.21]] [[2.21|Solution]] :2.22 :[[2.23]] [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 585d5a3f0818903312f00c6fd46a42b379e67a00 86 85 2020-09-03T21:07:03Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]] [[2.17|Solution]] :2.18 :[[2.19]] [[2.19|Solution]] :2.20 :[[2.21]] [[2.21|Solution]] :2.22 :[[2.23]] [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] a76dd45c7bdabfcbadf4f948530a258be5ab4f2d 87 86 2020-09-07T16:33:56Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26 :[[2.27]] [[2.27|Solution]] :2.28 :[[2.29]] [[2.29|Solution]] :2.30 :[[2.31]] [[2.31|Solution]] :2.32 :[[2.33]] [[2.33|Solution]] :2.34 :[[2.35]] [[2.35|Solution]] :2.36 ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 3b047645f0baacd57fc2e173e49aa641c59179ec 88 87 2020-09-07T16:40:22Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> (a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> (b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> (c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> (d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> (e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> (a) Is <math>3^n = O(2^n)</math>? <br> (b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> (c) Is <math>3^n = \Omega(2^n)</math>? <br> (d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. <math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math> ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 97ee07110c9b8c63559f7b319aab23c7b7c0f725 89 88 2020-09-07T16:45:19Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. ::#<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. <math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><math> \begin{array}{lcc} :: & A & B \\ ::(a) & n^{100} & 2^n \\ ::(b) & (\lg n)^{12} & \sqrt{n} \\ ::(c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ ::(d) & 10^n & 100^n \\ ::(e) & n^{\lg n} & (\lg n)^n \\ ::(f) & \lg{(n!)} & n \lg n \end{array} </math> ===Summations=== :2.37 :2.38 :2.39 :2.40 :2.41 :2.42 :2.43 ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 21d522c60630635ca8324bd0e4795d192ecbc576 90 89 2020-09-07T16:52:05Z Algowikiadmin 1 /* Summations */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. ::#<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. <math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><math> \begin{array}{lcc} :: & A & B \\ ::(a) & n^{100} & 2^n \\ ::(b) & (\lg n)^{12} & \sqrt{n} \\ ::(c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ ::(d) & 10^n & 100^n \\ ::(e) & n^{\lg n} & (\lg n)^n \\ ::(f) & \lg{(n!)} & n \lg n \end{array} </math> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 8cf24c396ac08f4fa2cf74313b03d06a5e660a91 91 90 2020-09-07T16:53:42Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. <math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><math> \begin{array}{lcc} :: & A & B \\ ::(a) & n^{100} & 2^n \\ ::(b) & (\lg n)^{12} & \sqrt{n} \\ ::(c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ ::(d) & 10^n & 100^n \\ ::(e) & n^{\lg n} & (\lg n)^n \\ ::(f) & \lg{(n!)} & n \lg n \end{array} </math> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 450b4eed8d5086244941cf79c0bf64114bda3ace 92 91 2020-09-07T16:54:41Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><math> \begin{array}{lcc} :: & A & B \\ ::(a) & n^{100} & 2^n \\ ::(b) & (\lg n)^{12} & \sqrt{n} \\ ::(c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ ::(d) & 10^n & 100^n \\ ::(e) & n^{\lg n} & (\lg n)^n \\ ::(f) & \lg{(n!)} & n \lg n \end{array} </math> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 540386df101097d917bd19d03dd2578da4259e6f 93 92 2020-09-07T16:56:15Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] ffec1f7685eb81e43cedf7eb7b5231047d96d565 94 93 2020-09-07T16:56:53Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 4a5c1680bab5a7214b929161d3bf1ea267d7048b 95 94 2020-09-07T16:57:44Z Algowikiadmin 1 /* Summations */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] 2d50d3910eca7240dfd4fcab8438734aa972cb47 96 95 2020-09-07T16:58:27Z Algowikiadmin 1 /* Summations */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44 :2.45 :2.46 :2.47 ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] c8d0caa17fd5f244b567932051e50eed46c1a115 97 96 2020-09-07T17:00:55Z Algowikiadmin 1 /* Logartihms */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48 :2.49 :2.50 :2.51 :2.52 :2.53 :2.54 :2.55 Back to [[Chapter List]] afdcb037e8d963d8c6edffd651714def433fdf0b 98 97 2020-09-07T17:14:38Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. <br> ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? <br> ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? <br> ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. <br> ::(a) Is <math>3^n = O(2^n)</math>? <br> ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? <br> ::(c) Is <math>3^n = \Omega(2^n)</math>? <br> ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] a7cd21d62163c295b31ad4b27918d77f49b8668c 99 98 2020-09-07T17:20:02Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] ebdcb7f58de78a7c4df85a0f222ccfc584637143 Chapter 3 0 6 100 15 2020-09-07T17:36:34Z Algowikiadmin 1 wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementray Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] a84718feab0d9342131f81d105ebbd952ed48126 Chapter 3 0 6 101 100 2020-09-07T17:36:58Z Algowikiadmin 1 /* Trees and Other Dictionary Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementray Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] e8b2af06fe1f49abb729e3108f5f127bf7b744e5 102 101 2020-09-07T17:37:11Z Algowikiadmin 1 /* Stacks, Queues, and Lists */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementray Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] cdb839325502155201de3aac36a6babff2d75281 103 102 2020-09-07T17:37:46Z Algowikiadmin 1 /* Elementray Data Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementary Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] 505c58e176fbf492d6019acf09dcb028885fc9fb 104 103 2020-09-07T17:38:02Z Algowikiadmin 1 /* Applications of Tree Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementary Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] a35564fb7760cba52845b937f791f090e5b748ea 105 104 2020-09-07T17:38:11Z Algowikiadmin 1 /* Implementation Projects */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementary Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] ef559e5d3c2201fcd56d403dcb1bfa3ef8bcc0a2 106 105 2020-09-07T17:38:27Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]] 3.2 [[3.3]] 3.4 [[3.5]] 3.6 [[3.7]] ===Elementary Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] 4cc8e5aaf14819e09eae0506d8a51bd0453fe97b Chapter 4 0 7 107 16 2020-09-07T18:10:16Z Algowikiadmin 1 wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]] :4.2 :[[4.3]] :4.4 :[[[4.5]] :4.6 :[[[4.7]] 4.8 [[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ==Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges]] :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 8877f1637559f13e1c2af90e0d47bc7fcc16d47d 108 107 2020-09-07T18:10:38Z Algowikiadmin 1 /* Mergesort= */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]] :4.2 :[[4.3]] :4.4 :[[[4.5]] :4.6 :[[[4.7]] 4.8 [[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges]] :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 5b62d87cb890db69c4b8e40268ae0a14f0362c4c 109 108 2020-09-07T18:10:49Z Algowikiadmin 1 /* Searching */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]] :4.2 :[[4.3]] :4.4 :[[[4.5]] :4.6 :[[[4.7]] 4.8 [[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 58f762b8234afc152117d615741422bf09f7cac5 110 109 2020-09-07T18:11:03Z Algowikiadmin 1 /* Applications of Sorting: Numbers */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]] :4.2 :[[4.3]] :4.4 :[[4.5]] :4.6 :[[4.7]] 4.8 [[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] c9793ba10a33ee83d751597f9b9c63fb945ef5dd 111 110 2020-09-07T18:11:24Z Algowikiadmin 1 /* Applications of Sorting: Numbers */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]] :4.2 :[[4.3]] :4.4 :[[4.5]] :4.6 :[[4.7]] :4.8 :[[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 2acf4a315143f2c85ed08cc2c946bde19b81328e Chapter 5 0 8 112 17 2020-09-07T18:16:13Z Algowikiadmin 1 wikitext text/x-wiki =Divide and Conquer= ===Binary Search=== :[[5.1]] :5.2 :[[5.3]] :5.4 :[[5.5]] :5.6 :[[5.7]] ===Divide and Conquer Algorithms=== :5.8 :[[5.9]] :5.10 :[[5.11]] ===Recurrence Relations=== :5.12 :[5.13]] :5.14 :[[5.15]] :5.16 Back to [[Chapter List]] f615f2c080e44e20ddae53f021946ef4822c1252 113 112 2020-09-07T18:16:27Z Algowikiadmin 1 /* Recurrence Relations */ wikitext text/x-wiki =Divide and Conquer= ===Binary Search=== :[[5.1]] :5.2 :[[5.3]] :5.4 :[[5.5]] :5.6 :[[5.7]] ===Divide and Conquer Algorithms=== :5.8 :[[5.9]] :5.10 :[[5.11]] ===Recurrence Relations=== :5.12 :[[5.13]] :5.14 :[[5.15]] :5.16 Back to [[Chapter List]] 4ab4cec608543e564050bd61026e7fe130b7ea74 Chapter 6 0 9 114 18 2020-09-07T18:20:35Z Algowikiadmin 1 wikitext text/x-wiki =Hashing and Randomized Algorithms= ===Propability=== :[[6.1]] :6.2 :[[6.3]] :6.4 ===Hashing=== :[[6.5]] :6.6 :[[6.7]] ===Randomized Algorithms=== :6.8 :[[6.9]] :6.10 :[[6.11]] :6.12 Back to [[Chapter List]] f40c948cf2b358e7cbca3879d216af1c0cee43c8 Chapter 7 0 10 115 19 2020-09-07T18:29:39Z Algowikiadmin 1 wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]] :7.2 ===Traversal== :[[7.3]] :7.4 :[[7.5]] :7.6 :[[7.7]] :7.8 :[[7.9]] :7.10 :[[7.11]] :7.12 ===Applications :[[7.13]] :7.14 :[[7.15]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] 8455a8a914b20bee3d87b179b58584e59ca940ca 116 115 2020-09-07T18:30:02Z Algowikiadmin 1 /* =Traversal */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]] :7.2 ===Traversal=== :[[7.3]] :7.4 :[[7.5]] :7.6 :[[7.7]] :7.8 :[[7.9]] :7.10 :[[7.11]] :7.12 ===Applications :[[7.13]] :7.14 :[[7.15]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] 686f8d2caee1e3efa402461da0c99102c1d991d3 117 116 2020-09-07T18:30:17Z Algowikiadmin 1 /* Traversal */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]] :7.2 ===Traversal=== :[[7.3]] :7.4 :[[7.5]] :7.6 :[[7.7]] :7.8 :[[7.9]] :7.10 :[[7.11]] :7.12 ===Applications=== :[[7.13]] :7.14 :[[7.15]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] f4ad42b904afbdf2baddb7f591bb8fa88b880cc7 Chapter 8 0 11 118 20 2020-09-07T18:57:07Z Algowikiadmin 1 wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating graph Algorithms=== :[[8.1]] ===Minimum Spanning Tree=== :8.2 :[[8.3]] :8.4 :[[8.5]] :8.6 :[[8.7]] :8.8 :[[8.9]] :8.10 :[[8.11]] :8.12 ===Union Find=== :[[8.13]] :8.14 ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] c7d6cdb2a93ee6fc12e4595d9840ef7118150bcb Chapter 9 0 12 119 21 2020-09-07T19:18:55Z Algowikiadmin 1 wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]] :9.2 :[[9.3]] :9.4 :[[9.5]] ===Backtracking=== :9.6 :[[9.7]] :9.8 :[[9.9]] :9.10 :[[9.11]] :9.12 :[[9.13]] :9.14 :[[9.15]] ===Games and Puzzles=== :9.16 :[[9.17]] :9.18 :[[9.19]] :9.20 ===Combinatorial Optimization=== :[[9.21]] :9.22 :[[9.23]] :9.24 :[[9.25]] :9.26 :[[9.27]] ===Interview Problems=== :9.28 :[[9.27]] :9.28 :[[9.29]] :9.30 :[[9.31]] :9.32 :[[9.33]] Back to [[Chapter List]] aa182cc522100885f159f9ba0ea53f9176391cd2 Chapter 10 0 13 120 22 2020-09-07T19:33:14Z Algowikiadmin 1 wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]] :10.2 :[[10.3]] :10.4 :[[10.5]] ===Edit Distance=== :10.6 :[[10.7]] :10.8 :[[10.9]] :10.10 ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 0b0459a06a12508c72c60018c3d72fc41bb16a09 Chapter 11 0 14 121 23 2020-09-07T19:45:02Z Algowikiadmin 1 wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]] :11.2 :[[11.3]] :11.4 :[[11.5]] :11.6 :[[11.7]] :11.8 :[[11.9]] ===Basic Reductions=== :11.10 :[[11.11]] :11.12 :[[11.13]] :11.14 :[[11.15]] :11.16 :[[11.17]] :11.18 :[[11.19]] :11.20 :[[11.21]] ===Creatvie Reductions=== :11.22 :[[11.23]] :11.24 :[[11.25]] :11.26 :[[11.27]] :11.28 :[[11.29]] :11.30 ===Algorithms for Special Cases=== :[[11.31]] :11.32 :[[11.33]] :11.34 :[[11.35]] Back to [[Chapter List]] 49c06244fee94c8c4a0880c70a5a7ac74d474e23 Chapter 12 0 15 122 24 2020-09-07T19:48:34Z Algowikiadmin 1 wikitext text/x-wiki =Dealing with Hard Problems=\ ===Special Cases of Hard Problems=== :[[12.1]] :12.2 :[[12.3]] ===Approximation Algorithms=== :12.4 :[[12.5]] :12.6 :[[12.7]] :12.8 :[[12.9]] :12.10 :[[12.11]] ===Combinatorial Optimization=== :12.12 :[[12.13]] :12.14 :[[12.15]] :12.16 :[[12.17]] :12.18 ==="Quantum" Computing=== :[[12.19]] :12.20 :[[12.21]] :12.22 Back to [[Chapter List]] d6962df237d443fe51e51a1b78338f8f234117c4 2.1 0 35 123 2020-09-08T19:25:40Z Algowikiadmin 1 Created page with " This loop can be expressed as the sum: <math> \sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\sum_{k=1}^{j}1 </math> Reducing this, sum by sum from the rhs: <math> \begin{align} &\sum_{i..." wikitext text/x-wiki This loop can be expressed as the sum: <math> \sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\sum_{k=1}^{j}1 </math> Reducing this, sum by sum from the rhs: <math> \begin{align} &\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\sum_{k=1}^{j}1 =\\ &\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}j =\\ &\sum_{i=1}^{n-1}\left(\sum_{j=1}^{n}j - \sum_{j=1}^{i}j\right) =\\ &\sum_{i=1}^{n-1}\left(\frac{n(n+1)}{2} - \frac{i(i+1)}{2}\right) =\\ &\frac{1}{2}\sum_{i=1}^{n-1}n^2+n-i^2-i =\\ &\frac{1}{2}\left((n-1)n^2 + (n-1)n - \left(\frac{n(n+1)(2n+1)}{6} - n^2\right) - \left(\frac{n(n+1)}{2} - n\right)\right) =\\ &f(n) = \frac{n(n(n+1))}{2} - \frac{n(n+1)(2n+1)}{12} - \frac{n(n+1)}{4} \end{align} </math> n=1 gives zero; and order is O((n^3)/3) == Alternative Derivation == <math> \begin{align} &mystery(n)=\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\sum_{k=1}^{j} 1\\ &=\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}j\\ \end{align} </math> Taking the following well-known formula for the sum of the integers <math>1</math> to <math>n</math>:- <math> \sum_{x=1}^{n}x = \frac{1}{2}n(n + 1) </math> And noting that:- <math> \sum_{j=i+1}^{n}j = \sum_{j=1}^{n}j - \sum_{j=1}^{i}j </math> We continue our derivation thus:- <math> \begin{align} &=\sum_{i=1}^{n-1}(\frac{1}{2}n(n+1) - \frac{1}{2}i(i+1))\\ &=\frac{1}{2}n(n-1)(n+1) - \frac{1}{2}\sum_{i=1}^{n-1}(i^2 + i)\\ \end{align} </math> Given our formula for the sum of integers, substituting <math>n-1</math> for <math>n</math> gives us:- <math> \sum_{x=1}^{n-1}x=\frac{1}{2}(n-1)((n-1)+1)=\frac{1}{2}n(n-1) </math> Thus continuing with our derivation:- <math> \begin{align} &=\frac{1}{2}n(n-1)(n+1)- \frac{1}{2}\frac{1}{2}n(n-1) - \frac{1}{2}\sum_{i=1}^{n-1}i^2\\ &=\frac{1}{2}n(n-1)(n + 1 - \frac{1}{2}) - \frac{1}{2}\sum_{i=1}^{n-1}i^2\\ &=\frac{1}{2}n(n-1)(n + \frac{1}{2}) - \frac{1}{2}\sum_{i=1}^{n-1}i^2\\ \end{align} </math> Let us consider the well-known formula for the sum of the squares of the integers between <math>1</math> and <math>n</math>:- <math> \sum_{x=1}^{n}x^2=\frac{1}{6}n(n+1)(2n+1) </math> As before, let us substitute <math>n-1</math> for <math>n</math>:- <math> \sum_{x=1}^{n-1}x^2=\frac{1}{6}(n-1)((n-1)+1)(2(n-1)+1)=\frac{1}{6}n(n-1)(2n-1) </math> Substituting this into our derivation gives us:- <math> \begin{align} &=\frac{1}{2}n(n-1)(n + \frac{1}{2}) - \frac{1}{2}\frac{1}{6}n(n-1)(2n - 1)\\ &=\frac{1}{2}n(n-1)(n + \frac{1}{2} - \frac{1}{6}(2n-1))\\ &=\frac{1}{2}n(n-1)(n + \frac{1}{2} - \frac{1}{3}n + \frac{1}{6})\\ &=\frac{1}{2}n(n-1)(\frac{2}{3}n + \frac{2}{3})\\ &=\frac{1}{3}n(n-1)(n+1) \end{align} </math> Thus the solution is <math>mystery(n)=\frac{1}{3}n(n-1)(n+1)</math>. This is the equivalent to the solution given by the previous derivation. The Big-Oh complexity of this function (ignoring constants) is O(n^3), as the RAM model dictates that each iteration of the increment of <math>r</math> is a time-step. We can ignore the first and last line's contribution to running time for the purposes of Big-Oh, as they do not contribute to the growth of the time taken with input size. Return to [[Chapter 2]] 65c8c1aedefd46371586f54d4712d870e57059e4 2.3 0 36 124 2020-09-09T18:49:32Z Algowikiadmin 1 Created page with "<math>f(n) = (((n^2)(n+1)^2)/8) + n(n+1)(2n+1)/12</math> ---- This problem does appear to break down into a series of nested summations: <math> \displaystyle\sum_{i=1}^{n}\te..." wikitext text/x-wiki <math>f(n) = (((n^2)(n+1)^2)/8) + n(n+1)(2n+1)/12</math> ---- This problem does appear to break down into a series of nested summations: <math> \displaystyle\sum_{i=1}^{n}\text{ } \displaystyle\sum_{j=1}^{i}\text{ } \displaystyle\sum_{k=j}^{i+j}\text{ } \displaystyle\sum_{l=1}^{j+i-k}1 </math> In the last summation, the formula is independent of the iterator, which translates into adding the value 1, <math>j+i-k</math> times: <math> \displaystyle\sum_{i=1}^{n} \displaystyle\sum_{j=1}^{i} \displaystyle\sum_{k=j}^{i+j}(j+i-k) </math> Now the third summation goes from <math>j</math> to <math>i+j</math> the formula on closer examination reveals that <math>\displaystyle\sum_{k=j}^{i+j}(j+i-k)</math> is <math> \displaystyle\sum_{k=1}^{i}(k) </math> which is equal to <math>i*(i+1)/2</math> Since <math>\displaystyle\sum_{k=start}^{end}(end-k) = \displaystyle\sum_{k=1}^{end-start}(k) </math> So the summation boils down to <math> \displaystyle\sum_{i=1}^{n} \displaystyle\sum_{j=1}^{i} (i*(i+1)/2) </math> The formula in the second summation is independent of the iterator, which translates to adding <math>i*(i+1)/2</math>, <math>i</math> times. <math> \displaystyle\sum_{i=1}^{n}(i^2 * (i+1)/2) </math> which is <math> \displaystyle\sum_{i=1}^{n}((i^3 + i^2)/2) </math> <math>\frac{1}{2} \left(\Sigma r^3 + \Sigma r^2\right) = \frac{1}{2}\left(\frac{n^2\left(n+1\right)^2}{4} + \frac {n \left(n+1\right)\left(2n+1\right)}{6}\right) = \frac{n^2\left(n+1\right)^2}{8} + \frac {n \left(n+1\right)\left(2n+1\right)}{12}</math> Time Complexity = O<math>({n}^{4})</math> ---- Back to [[Chapter 2]] a4758e39e8778b21982b37dc1289ecedc1ce88ad 2.5 0 37 125 2020-09-09T18:50:52Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] f50fc778da5f9aa821fdfcae350e1b243dc74248 2.7 0 38 126 2020-09-09T18:51:59Z Algowikiadmin 1 Created page with "'''n = 1''' The single element array is already its max. Loop is not entered. Max is returned Let for '''n=k''', the algorithm is true For '''n = k+1''' ,two cases arise :..." wikitext text/x-wiki '''n = 1''' The single element array is already its max. Loop is not entered. Max is returned Let for '''n=k''', the algorithm is true For '''n = k+1''' ,two cases arise : '''1)''' a[k+1] is max '''2)''' a[k+1] is not max. If '''1)''' holds then at the last iteration when i = k+1, m = a[k+1] is assigned. Hence max is returned Else if '''2)''' holds then we are left with the task of finding the max among '''n = k''' elements, which we already assumed that the algorithm does correctly. Back to [[Chater 2]] 33bdebeab594bd1fbe3fa1d3be053843518d1955 127 126 2020-09-09T18:52:16Z Algowikiadmin 1 wikitext text/x-wiki '''n = 1''' The single element array is already its max. Loop is not entered. Max is returned Let for '''n=k''', the algorithm is true For '''n = k+1''' ,two cases arise : '''1)''' a[k+1] is max '''2)''' a[k+1] is not max. If '''1)''' holds then at the last iteration when i = k+1, m = a[k+1] is assigned. Hence max is returned Else if '''2)''' holds then we are left with the task of finding the max among '''n = k''' elements, which we already assumed that the algorithm does correctly. Back to [[Chapter 2]] de98267630d517d789622931cd01885daaa7e9e8 2.9 0 39 128 2020-09-09T18:53:36Z Algowikiadmin 1 Created page with "=2-8.= For each of the following pairs of functions, either <math>f(n)</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Th..." wikitext text/x-wiki =2-8.= For each of the following pairs of functions, either <math>f(n)</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. ==<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math>== Answer: <math>\log n^2 = \Theta (\log n + 5)</math> Solution: <math>\log n^2 = 2 \times \log n</math> <math>2 \times \log n \le 2 \times \log n + 10</math> <math>\log n^2 \le 2 (\log n + 5)</math> <math>\log n^2 \le C (\log n + 5)</math> (where <math>C=2</math>) <math>\log n^2 = O (\log n + 5)</math> Also: <math>\log n + 5 \le \log n + 5 \log n</math> <math>\log n + 5 \le 6 \log n</math> <math>\log n + 5 \le 3 \times 2 \log n</math> <math>3 \times \log n^2 \ge \log n + 5 </math> <math>\log n^2 \ge C \times (\log n + 5)</math> (Where <math>C =\frac{1}{3}</math>) <math>log n^2 = \Omega (\log n + 5)</math> And therefore: <math>log n^2 = \Theta (\log n + 5)</math> ==<math>f(n)=\sqrt{n}</math>; <math>g(n)=\log(n^2)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>g(n) = \log (n^2) = 2 * \log (n)</math> <math>\lim_{n \to \infty} \frac{\sqrt{n}}{2 * log(n)} = 2 * \lim_{n \to \infty} \frac{\sqrt{n}}{log(n)} = \infty</math> ==<math>f(n)=\log^2(n)</math>; <math>g(n)=\log (n)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{log^2(n)}{log(n)} = \lim_{n \to \infty} log(n) = \infty</math> ==<math>f(n)=n</math>; <math>g(n)=\log^2(n)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{n}{\log^2(n)} = \lim_{n \to \infty} ((\frac{\sqrt{n}}{\log(n)})^2) = (\lim_{n \to \infty} \frac{\sqrt{n}}{\log(n)})^2 = \infty</math> ==<math>f(n)=n * \log(n) + n</math>; <math>g(n)=\log (n)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{n * \log(n) + n}{log(n)} = \lim_{n \to \infty} (\frac{n * \log(n)}{log(n)} + \frac{n}{log(n)}) = \lim_{n \to \infty} (n + \frac{n}{log(n)}) = \infty</math> ==<math>f(n)=10</math>; <math>g(n)=\log (10)</math>== Answer: <math>f(n) = \Theta(g(n))</math> Solution: Both are constants. Constants are always within a constant factor, <math>c</math>, of each other (as <math>n \rightarrow \infty</math>). ==<math>f(n)=2^n</math>; <math>g(n)=10n^2</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math> \begin{align} &\lim_{n \to \infty} \frac{2^n}{10n^2} \\ &\implies \frac{1}{10} \left(\lim_{n \to \infty} \frac{2^n}{n^2}\right) \\ &\implies \frac{1}{10} \left(\lim_{n \to \infty} \frac{\ln(2)\cdot 2^n}{2 \cdot n}\right) \text{L'Hopital's Rule} \\ &\implies \frac{1}{10} \left(\lim_{n \to \infty} \frac{2\ln(2)\cdot 2^n}{2}\right) \text{L'Hopital's Rule} \\ &\implies \frac{\ln(2)}{10} \lim_{n \to \infty} 2^n \\ &\implies \frac{\ln(2)}{10} \lim_{n \to \infty} 2^n = \infty \end{align} </math> ==<math>f(n)=2^n</math>; <math>g(n)=3^n</math>== Answer: <math>f(n) = O(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{2^n}{3^n} = \lim_{n \to \infty} (\frac{2}{3})^n = 0</math> Back to [[Chapter 2]] 18f783b4c31c7eca08792c2ba08e4d482701e56b 130 128 2020-09-09T19:00:28Z Algowikiadmin 1 /* 2-8. */ wikitext text/x-wiki For each of the following pairs of functions, either <math>f(n)</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. ==<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math>== Answer: <math>\log n^2 = \Theta (\log n + 5)</math> Solution: <math>\log n^2 = 2 \times \log n</math> <math>2 \times \log n \le 2 \times \log n + 10</math> <math>\log n^2 \le 2 (\log n + 5)</math> <math>\log n^2 \le C (\log n + 5)</math> (where <math>C=2</math>) <math>\log n^2 = O (\log n + 5)</math> Also: <math>\log n + 5 \le \log n + 5 \log n</math> <math>\log n + 5 \le 6 \log n</math> <math>\log n + 5 \le 3 \times 2 \log n</math> <math>3 \times \log n^2 \ge \log n + 5 </math> <math>\log n^2 \ge C \times (\log n + 5)</math> (Where <math>C =\frac{1}{3}</math>) <math>log n^2 = \Omega (\log n + 5)</math> And therefore: <math>log n^2 = \Theta (\log n + 5)</math> ==<math>f(n)=\sqrt{n}</math>; <math>g(n)=\log(n^2)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>g(n) = \log (n^2) = 2 * \log (n)</math> <math>\lim_{n \to \infty} \frac{\sqrt{n}}{2 * log(n)} = 2 * \lim_{n \to \infty} \frac{\sqrt{n}}{log(n)} = \infty</math> ==<math>f(n)=\log^2(n)</math>; <math>g(n)=\log (n)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{log^2(n)}{log(n)} = \lim_{n \to \infty} log(n) = \infty</math> ==<math>f(n)=n</math>; <math>g(n)=\log^2(n)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{n}{\log^2(n)} = \lim_{n \to \infty} ((\frac{\sqrt{n}}{\log(n)})^2) = (\lim_{n \to \infty} \frac{\sqrt{n}}{\log(n)})^2 = \infty</math> ==<math>f(n)=n * \log(n) + n</math>; <math>g(n)=\log (n)</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{n * \log(n) + n}{log(n)} = \lim_{n \to \infty} (\frac{n * \log(n)}{log(n)} + \frac{n}{log(n)}) = \lim_{n \to \infty} (n + \frac{n}{log(n)}) = \infty</math> ==<math>f(n)=10</math>; <math>g(n)=\log (10)</math>== Answer: <math>f(n) = \Theta(g(n))</math> Solution: Both are constants. Constants are always within a constant factor, <math>c</math>, of each other (as <math>n \rightarrow \infty</math>). ==<math>f(n)=2^n</math>; <math>g(n)=10n^2</math>== Answer: <math>f(n) = \Omega(g(n))</math> Solution: <math> \begin{align} &\lim_{n \to \infty} \frac{2^n}{10n^2} \\ &\implies \frac{1}{10} \left(\lim_{n \to \infty} \frac{2^n}{n^2}\right) \\ &\implies \frac{1}{10} \left(\lim_{n \to \infty} \frac{\ln(2)\cdot 2^n}{2 \cdot n}\right) \text{L'Hopital's Rule} \\ &\implies \frac{1}{10} \left(\lim_{n \to \infty} \frac{2\ln(2)\cdot 2^n}{2}\right) \text{L'Hopital's Rule} \\ &\implies \frac{\ln(2)}{10} \lim_{n \to \infty} 2^n \\ &\implies \frac{\ln(2)}{10} \lim_{n \to \infty} 2^n = \infty \end{align} </math> ==<math>f(n)=2^n</math>; <math>g(n)=3^n</math>== Answer: <math>f(n) = O(g(n))</math> Solution: <math>\lim_{n \to \infty} \frac{2^n}{3^n} = \lim_{n \to \infty} (\frac{2}{3})^n = 0</math> Back to [[Chapter 2]] 3e563c85ed5634692a65dea4029c220866521b9b Chapter 2 0 4 129 99 2020-09-09T18:58:28Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n #<math>f(n) = 2^(klog n), g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^(2n)</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] cf175582e33f6273c0af5c31f71a37ffb202d02b 131 129 2020-09-09T21:54:21Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2^(klog n), g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^(2n)</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 40423441366c558e3e933ed527ee3468274d43df 132 131 2020-09-10T19:29:51Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2<sup>klog n</sup>, g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^(2n)</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 7ed15a0fff3eb09e61eb5c4ce63a1fa748c96d4f 133 132 2020-09-10T19:34:56Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2^{klog n}, g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^{2n}</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 98dbc96718e6eed314d4d15049f236071bb9ffd5 2.11 0 40 134 2020-09-10T19:38:27Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.13 0 41 135 2020-09-10T19:39:20Z Algowikiadmin 1 Created page with "because <math> n^2 <= 2^n </math> for every n greater than 4 . Hence, we can say that <math> n^2 < = C* 2^n </math> for every n>=4 and so <math> n^2 = O(2^n)</math>. Back t..." wikitext text/x-wiki because <math> n^2 <= 2^n </math> for every n greater than 4 . Hence, we can say that <math> n^2 < = C* 2^n </math> for every n>=4 and so <math> n^2 = O(2^n)</math>. Back to [[Chapter 2]] aeea2f3f1f6a23778f6cf22371429c573e9c2a1a 2.15 0 42 136 2020-09-10T19:40:21Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.17 0 43 137 2020-09-10T19:41:00Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.55 0 44 138 2020-09-10T19:41:59Z Algowikiadmin 1 Created page with "This problem is a famous game-theoretical scenario called the pirate game (http://en.wikipedia.org/wiki/Pirate_game). Assume the senior pirate gets to vote. Where there is on..." wikitext text/x-wiki This problem is a famous game-theoretical scenario called the pirate game (http://en.wikipedia.org/wiki/Pirate_game). Assume the senior pirate gets to vote. Where there is only 1 indivisible dollar: 2 pirates - The senior pirate gets it. 3+ pirates - The least senior pirate gets it. In general, every 2 + 2^K (k >= 1) pirate will survive, while the others will die '''Another Answer''' : Assume senior pirate also gets to vote. Based on pirates inherent tendency - '''a.''' They want to kill other pirates even if it does not affect their outcome - 2 pirates: Senior gets it. 3 pirates: Least-senior pirate gets it because the 2nd-least-senior pirate will vote for kill; so in order to live, he/she must give the gold to the least-senior pirate. 4 pirates: 2nd or 3rd-least-senior pirate will get gold (equally likely) because 1st will vote for kill irrespective of whether he/she gets the gold or not, and the 2nd or 3rd-least-senior pirate will vote for the plan if he/she gets the gold respectively. 5 pirates - This pirate is certainly going to die regardless of his/her proposal since there will be at least 3 pirates that don't get the gold and thus vote to kill him/her. This will eventually lead to the above situation where the 2nd or 3rd-least-senior pirate will receive the gold. 6 pirates: The 5th-least-senior pirate will vote for whatever plan the most senior pirate proposes—if the proposal falls down to him/her, then he/she is guaranteed to die as explained above. In order to get the last vote, the most senior pirate can give the gold to any of the remaining four pirates. The reason for each is below—recall, the desire to have the gold surpasses the desire to kill another pirate (e.g., if Pirate A has a 50% chance of getting the gold without killing anyone vs. a 49% chance of getting the gold and killing everyone else, he/she will choose the former): • 4th-least-senior pirate: would agree to receive the coin, because as mentioned before, if the 5th-least-senior pirate decides, he/she will die. This will eventually result in this pirate having to give away the coin in order to live as described in the "4 pirates" situation. • 3rd-least-senior pirate: would agree to receive the coin since there would only be a 50% chance he/she would get it as described in the eventually inevitable "4 pirates" situation. • 2nd-least-senior pirate: same reasoning as above. • Least senior pirate: the least senior pirate would agree to receive the coin since he/she would have a 0% chance to receive the gold as described in the eventually inevitable "4 pirates" situation. Thus the result would be that no one dies and either the first, second, third, or fourth-least-senior pirate would receive the coin. '''b.''' They want as many other pirates to live if it doesn't affect their outcome - Senior most pirate gets the gold. Back to [[Chapter 2]] 2f7e2a0f2f8bc6f5c1206dc6205252b673a37284 2.53 0 45 139 2020-09-10T19:43:00Z Algowikiadmin 1 Created page with "Some incorrect answers were reached. These have been moved to the discussion, with explanation of where the argument goes wrong. The correct answer: Assuming pairwise merges..." wikitext text/x-wiki Some incorrect answers were reached. These have been moved to the discussion, with explanation of where the argument goes wrong. The correct answer: Assuming pairwise merges (no k-way merges) we have <math>n-1</math> merge stages where each set of companies is generated from the previous stage (the merged pair being one company). If we characterize each complete merge as different if two companies merge at different stages then we are looking at combinations. In the first stage we have <math>\binom{n}{2}</math> ways to choose a merge pair and a new set for the next stage. Thus, we have the Cartesian product of each stage set and we can calculate the size as: <math>\prod_{i=2}^{n} \frac{i(i-1)}{2} = \frac{n! (n-1)!}{2^{n-1}}</math> Proof by induction follows easily as long as we agree with the counting process for first non-trivial basis (i.e. <math>n=3</math> where our formula indicates there are three different possible merges): <pre> {a,b,c} -> {ab, c} -> {abc} -> {a, bc} -> {abc} -> {ac, b} -> {abc} </pre> And, since we have three paths we can say that's three merges -- this counts a merge step as the same if it results in the same set of companies going on to the next stage. But, instead if we count the actual merge operations: <pre> {a, b, c} -> {a+b, c}, {(a+b) + c}, {a, b+c}, {a + (b+c)}, {a+c, b}, {(a+c) + b} </pre> then we need to change the denominator in our previous formula: <math>2^{n-1} \rightarrow 2^{n-2}</math> to get six steps. __________________________________________________________________________________ Solution above is correct given its assumptions but this problem can be more interesting. According to the examples above, {a, b, c, d} -> {ab, c, d} -> {ab, cd} is different from {a, b, c, d} -> {a, b, cd} -> {ab, cd}. I would argue that the two paths are the same. I would define two path to be the same if each employee of each company sees their own company merging sequentially with the same companies in both paths. In the example above, the employees of company c see that they merged with d and then with ab in both paths to full merger. Think about this. It is kinda complicated. So if we write down an individual merge sequence for each of the companies, then two paths are equal if all of their individual merge sequences are equal. Now how to calculate the number of paths? Pick company number N and assume it's IBM. Now consider IBM's individual merging path. It could be {IBM} -> {IBM, DELL}, or {IBM} -> {IBM, DELL, Apple} or {IBM} -> {IBM, Google, Netflix, Amazon}. The aspect I'm emphasizing here is that in its first merger, IBM could merge with 1 to n-1 many companies. We can compute all of the paths by adding up these scenarios. So let's say IBM will be merging with i companies in its first merger. How many paths would we have now. Let's define f(n) to be the number of merger paths for n companies (the number we are trying to find). Then there are <math> {n-1 \choose i} </math> ways to pick the i companies IBM will be merging with. Those companies have to merge only with each other before they merge with IBM and there are f(i) ways to do that. After IBM has merge with the i companies, then we have n-i companies left to merge (counting the big set of i companies as one company), and there are f(n-i) ways to do that. So we get the recursive equation: <math> f(n) = \sum^{n-1}_{i=1} f(i)f(n-i){n-1 \choose i} </math>. Back to [[Chapter 2]] 1b068d313a42527d3ae76d8c024a705bb76d7d57 2.51 0 46 140 2020-09-10T19:43:59Z Algowikiadmin 1 Created page with "<pre> 1) Find an empty bag (labeled "E") 2) Place 1 coin from bag 1 into E 3) Place 2 coins from bag 2 into E ... 10) Place 9 coins from bag 9 into E 11) Place 10 coins from b..." wikitext text/x-wiki <pre> 1) Find an empty bag (labeled "E") 2) Place 1 coin from bag 1 into E 3) Place 2 coins from bag 2 into E ... 10) Place 9 coins from bag 9 into E 11) Place 10 coins from bag 10 into E 12) Weigh bag E on your digital scale </pre> If all coins were 10 grams, the bag would weigh 550 grams. Thus, <math>550 - weight</math> will tell you how many coins are too light. Since this number of coins correlates to the bag from which the coins came, you now know which bag contains the light coins. Back to [[Chapter 2]] 0f648c680c617dbc70e97ca98e8cd7b31c547108 2.49 0 47 141 2020-09-10T19:44:46Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.47 0 48 142 2020-09-10T19:45:40Z Algowikiadmin 1 Created page with "Change the assumptions of the proof. The paper mentioned is "S. Skiena. Encroaching lists as a measure of presortedness. BIT, 28:775-784, 1988." '''Other solution :''' <m..." wikitext text/x-wiki Change the assumptions of the proof. The paper mentioned is "S. Skiena. Encroaching lists as a measure of presortedness. BIT, 28:775-784, 1988." '''Other solution :''' <math> O(nlog(√n)) </math> and <math> O(nlog(n)) </math> belongs to same class of function with respect to Big O notation. There is no difference between them other than a constant factor. <math>\lim_{x\to\infty} (nlog(√n)) / (nlog(n))</math> = <math>\lim_{x\to\infty} 1/2*(nlog(n))/(nlog(n)) </math> = <math>1/2</math> Back to [[Chapter 2]] 9588975be955c5206672f18b38816b237b482637 143 142 2020-09-10T19:49:09Z Algowikiadmin 1 wikitext text/x-wiki Change the assumptions of the proof. The paper mentioned is "S. Skiena. Encroaching lists as a measure of presortedness. BIT, 28:775-784, 1988." '''Other solution :''' <math> O(nlog(\sqrtn)) </math> and <math> O(nlog(n)) </math> belongs to same class of function with respect to Big O notation. There is no difference between them other than a constant factor. <math>\lim_{x\to\infty} (blog(/sqrtn)) / (nlog(n))</math> = <math>\lim_{x\to\infty} 1/2*(nlog(n))/(nlog(n)) </math> = <math>1/2</math> Back to [[Chapter 2]] 1d55d1e45e7b69c5fc0cf1ebcd702175a63345fa 144 143 2020-09-10T19:50:16Z Algowikiadmin 1 wikitext text/x-wiki Change the assumptions of the proof. The paper mentioned is "S. Skiena. Encroaching lists as a measure of presortedness. BIT, 28:775-784, 1988." '''Other solution :''' <math> O(nlog(\sqrt{n})) </math> and <math> O(nlog(n)) </math> belongs to same class of function with respect to Big O notation. There is no difference between them other than a constant factor. <math>\lim_{x\to\infty} (nlog(\sqrt{n})) / (nlog(n))</math> = <math>\lim_{x\to\infty} 1/2*(nlog(n))/(nlog(n)) </math> = <math>1/2</math> Back to [[Chapter 2]] 1f1856b69126441390935dfd0d931563ca76777b 2.45 0 49 145 2020-09-10T19:51:00Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.43 0 50 146 2020-09-10T19:51:57Z Algowikiadmin 1 Created page with "X=n-digit number (abcdefghijklmn), y= n-digit number (ABCDEFGHIJKLMN) say X * y = X * N + X * M0 + X * L00 + X * K000 + .... + X * B000000000000 + X * A0000000000000 With ea..." wikitext text/x-wiki X=n-digit number (abcdefghijklmn), y= n-digit number (ABCDEFGHIJKLMN) say X * y = X * N + X * M0 + X * L00 + X * K000 + .... + X * B000000000000 + X * A0000000000000 With each multiple consisting of further digit by digit multiplications as X * N = n * N + m0 * M + l00 * L + k000 * K + .... + b000000000000 * B + a0000000000000 * A i.e. n multiples, each generated with n digit by digit multiplications and additions hence, <math>O(n^2)</math> Back to [[Chapter 2]] 47928a3f5ae18edb0de64a6da636a4e64cc973df 2.41 0 51 147 2020-09-10T19:53:47Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.39 0 52 148 2020-09-10T19:54:45Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.37 0 53 149 2020-09-10T19:55:26Z Algowikiadmin 1 Created page with "On careful observation , one can see that the sum of any row is just <math>3^{n-1}</math> this is the sum for the series . This can even be computed using a series as shown b..." wikitext text/x-wiki On careful observation , one can see that the sum of any row is just <math>3^{n-1}</math> this is the sum for the series . This can even be computed using a series as shown below 1=a0 1 1 1 a1 a0 a2 now a(mid) of the next line becomes the a(0) +a(1) +a(2) that it this mid element is always the sum of the middle three elements '''Alternate explanation :''' Every element in the current row will be used 3 times in next row; once directly below it, and also in left and right element below it. So sum of elements in <math>(n+1)</math>th row will be 3*(sum of elements in <math>n</math>th row). Hence, S(n) = <math>3^{n-1}</math> (Base case : For 1st row, sum is 1) Back to [[Chapter 2]] ffa5ee455eb3a7119d0ad46d485ab8bd8d7f7f88 150 149 2020-09-10T19:55:44Z Algowikiadmin 1 wikitext text/x-wiki On careful observation, one can see that the sum of any row is just <math>3^{n-1}</math> this is the sum for the series. This can even be computed using a series as shown below 1=a0 1 1 1 a1 a0 a2 now a(mid) of the next line becomes the a(0) +a(1) +a(2) that it this mid element is always the sum of the middle three elements '''Alternate explanation :''' Every element in the current row will be used 3 times in the next row; once directly below it, and also in left and right element below it. So sum of elements in <math>(n+1)</math>th row will be 3*(sum of elements in <math>n</math>th row). Hence, S(n) = <math>3^{n-1}</math> (Base case : For 1st row, sum is 1) Back to [[Chapter 2]] 3e0c786de210f86454e74a4c75339e321162c866 2.35 0 54 151 2020-09-10T19:56:23Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.33 0 55 152 2020-09-10T19:57:19Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.31 0 56 153 2020-09-10T19:57:55Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.29 0 57 154 2020-09-10T19:58:30Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.27 0 58 155 2020-09-10T19:59:20Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.23 0 59 156 2020-09-10T19:59:51Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.25 0 60 157 2020-09-10T20:00:19Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.19 0 61 158 2020-09-10T20:00:49Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 2.21 0 62 159 2020-09-10T20:01:20Z Algowikiadmin 1 Created page with " Back to [[Chapter 2]]" wikitext text/x-wiki Back to [[Chapter 2]] 33134726b0c9d912ce836678504ea88422a311f0 Chapter 2 0 4 160 133 2020-09-10T20:02:27Z Algowikiadmin 1 /* Program Analysis */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>\Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2^{klog n}, g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^{2n}</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24 :[[2.25]] [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 1fddc06034d2de7f85f2feb7dc34ce1dba14e574 161 160 2020-09-10T20:14:52Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>\Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2^{klog n}, g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^{2n}</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array} n^{\pi} & {\pi}^n & \binom{n}{5} & \sqrt{2\sqrt{n}} \\ \binom{n}{n-4} & 2^{log^4n} & n^{5(logn)^2} $ n^4\binom{n}{n-4} \\ \end{array}</math> </center> :[[2.25]]. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} FILL IN \end{array}</math> </center> [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 2c678b1c7be7a07ddeb26a5e5d8eaa2808ded0f2 Chapter 1 0 3 162 75 2020-09-10T20:17:10Z Algowikiadmin 1 wikitext text/x-wiki Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number. =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. [[1.1|Solution]] :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. [[1.3|Solution]] :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. [[1.5|Solution]] :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. [[1.7|Solution]] ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> [[1.9|Solution]] :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. [[1.11|Solution]] ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. [[1.13|Solution]] :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> [[1.15|Solution]] :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> [[1.17|Solution]] :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. [[1.19|Solution]] :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? [[1.21|Solution]] :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. [[1.23|Solution]] :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. [[1.25|Solution]] :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? [[1.27|Solution]] :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? [[1.29|Solution]] ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. [[1.31|Solution]] ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. [[1.33|Solution]] :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? [[1.35|Solution]] :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? [[1.37|Solution]] :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 5d9aa8fcf4b2825cda3af597916108cd330f2d45 Chapter 12 0 15 163 122 2020-09-10T20:30:41Z Algowikiadmin 1 /* Special Cases of Hard Problems */ wikitext text/x-wiki =Dealing with Hard Problems=\ ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4 :[[12.5]] :12.6 :[[12.7]] :12.8 :[[12.9]] :12.10 :[[12.11]] ===Combinatorial Optimization=== :12.12 :[[12.13]] :12.14 :[[12.15]] :12.16 :[[12.17]] :12.18 ==="Quantum" Computing=== :[[12.19]] :12.20 :[[12.21]] :12.22 Back to [[Chapter List]] a6fa6e9d9f311f85ca84adcac28c14610e531df6 164 163 2020-09-10T20:45:17Z Algowikiadmin 1 /* Approximation Algorithms */ wikitext text/x-wiki =Dealing with Hard Problems=\ ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution. :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal. [[12.5|Solution]] :12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut. :[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram. :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution. [[12.7|Solution]] :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal. :[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set. [[12.9|Solution]] :12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>. :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph. [[12.11|Solution]] ===Combinatorial Optimization=== :12.12 :[[12.13]] :12.14 :[[12.15]] :12.16 :[[12.17]] :12.18 ==="Quantum" Computing=== :[[12.19]] :12.20 :[[12.21]] :12.22 Back to [[Chapter List]] ad411fd08e131a1b1eae04b4022024c1bdf9e09c 165 164 2020-09-10T20:57:33Z Algowikiadmin 1 /* Combinatorial Optimization */ wikitext text/x-wiki =Dealing with Hard Problems=\ ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution. :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal. [[12.5|Solution]] :12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut. :[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram. :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution. [[12.7|Solution]] :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal. :[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set. [[12.9|Solution]] :12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>. :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph. [[12.11|Solution]] ===Combinatorial Optimization=== For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice? :12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470). :[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537). [[12.13|Solution]] :12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586). :[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604). [[12.15|Solution]] :12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608). :[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618). [[12.7|Solution]] :12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678). ==="Quantum" Computing=== :[[12.19]] :12.20 :[[12.21]] :12.22 Back to [[Chapter List]] 9b7b6bbe633253973cd429062462affafb377820 166 165 2020-09-10T21:03:44Z Algowikiadmin 1 /* "Quantum" Computing */ wikitext text/x-wiki =Dealing with Hard Problems=\ ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution. :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal. [[12.5|Solution]] :12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut. :[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram. :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution. [[12.7|Solution]] :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal. :[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set. [[12.9|Solution]] :12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>. :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph. [[12.11|Solution]] ===Combinatorial Optimization=== For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice? :12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470). :[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537). [[12.13|Solution]] :12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586). :[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604). [[12.15|Solution]] :12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608). :[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618). [[12.7|Solution]] :12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678). ==="Quantum" Computing=== :[[12.19]]. Consider an <math>n</math> qubit “quantum” system <math>Q</math>, where each of the <math>N = 2^n</math> states start out with equal probability <math>p(i) = 1/2^n</math>. Say the ''Jack''<math>(Q, 0^n)</math> operation doubles the probability of the state where all qubits are zero. How many calls to this ''Jack'' operation are necessary until the probability of sampling this null state becomes ≥ 1/2? [[12.19|Solution]] :12.20. For the satisfiability problem, construct (a) an instance on <math>n</math> variables that has exactly one solution, and (b) an instance on <math>n</math> variables that has exactly <math>2^n</math> different solutions. :[[12.21]]. Consider the first ten multiples of 11, namely 11, 22, . . . 110. Pick two of them (<math>x</math> and <math>y</math>) at random. What is the probability that ''gcd''<math>(x, y) = 11</math>? [[12.21|Solution]] :12.22. IBM quantum computing (https://www.ibm.com/quantum-computing/) offers the opportunity to program a quantum computing simulator. Take a look at an example quantum computing program and run it to see what happens. Back to [[Chapter List]] e2d454456cf8e4bc43c6bafd9cbef7f2004aaa0f 173 166 2020-09-10T21:10:21Z Algowikiadmin 1 /* Combinatorial Optimization */ wikitext text/x-wiki =Dealing with Hard Problems=\ ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution. :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal. [[12.5|Solution]] :12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut. :[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram. :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution. [[12.7|Solution]] :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal. :[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set. [[12.9|Solution]] :12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>. :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph. [[12.11|Solution]] ===Combinatorial Optimization=== For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice? :12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470). :[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537). [[12.13|Solution]] :12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586). :[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604). [[12.15|Solution]] :12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608). :[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618). [[12.17|Solution]] :12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678). ==="Quantum" Computing=== :[[12.19]]. Consider an <math>n</math> qubit “quantum” system <math>Q</math>, where each of the <math>N = 2^n</math> states start out with equal probability <math>p(i) = 1/2^n</math>. Say the ''Jack''<math>(Q, 0^n)</math> operation doubles the probability of the state where all qubits are zero. How many calls to this ''Jack'' operation are necessary until the probability of sampling this null state becomes ≥ 1/2? [[12.19|Solution]] :12.20. For the satisfiability problem, construct (a) an instance on <math>n</math> variables that has exactly one solution, and (b) an instance on <math>n</math> variables that has exactly <math>2^n</math> different solutions. :[[12.21]]. Consider the first ten multiples of 11, namely 11, 22, . . . 110. Pick two of them (<math>x</math> and <math>y</math>) at random. What is the probability that ''gcd''<math>(x, y) = 11</math>? [[12.21|Solution]] :12.22. IBM quantum computing (https://www.ibm.com/quantum-computing/) offers the opportunity to program a quantum computing simulator. Take a look at an example quantum computing program and run it to see what happens. Back to [[Chapter List]] fe2d3d3d9f30ac2acb2b8b7c322aea8c47a25509 179 173 2020-09-10T21:11:49Z Algowikiadmin 1 wikitext text/x-wiki =Dealing with Hard Problems= ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution. :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal. [[12.5|Solution]] :12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut. :[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram. :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution. [[12.7|Solution]] :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal. :[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set. [[12.9|Solution]] :12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>. :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph. [[12.11|Solution]] ===Combinatorial Optimization=== For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice? :12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470). :[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537). [[12.13|Solution]] :12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586). :[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604). [[12.15|Solution]] :12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608). :[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618). [[12.17|Solution]] :12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678). ==="Quantum" Computing=== :[[12.19]]. Consider an <math>n</math> qubit “quantum” system <math>Q</math>, where each of the <math>N = 2^n</math> states start out with equal probability <math>p(i) = 1/2^n</math>. Say the ''Jack''<math>(Q, 0^n)</math> operation doubles the probability of the state where all qubits are zero. How many calls to this ''Jack'' operation are necessary until the probability of sampling this null state becomes ≥ 1/2? [[12.19|Solution]] :12.20. For the satisfiability problem, construct (a) an instance on <math>n</math> variables that has exactly one solution, and (b) an instance on <math>n</math> variables that has exactly <math>2^n</math> different solutions. :[[12.21]]. Consider the first ten multiples of 11, namely 11, 22, . . . 110. Pick two of them (<math>x</math> and <math>y</math>) at random. What is the probability that ''gcd''<math>(x, y) = 11</math>? [[12.21|Solution]] :12.22. IBM quantum computing (https://www.ibm.com/quantum-computing/) offers the opportunity to program a quantum computing simulator. Take a look at an example quantum computing program and run it to see what happens. Back to [[Chapter List]] 9ed3ef2cfc1f3ebac226557eb19791450473c8e7 12.1 0 63 167 2020-09-10T21:08:23Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.3 0 64 168 2020-09-10T21:08:35Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.5 0 65 169 2020-09-10T21:08:56Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.7 0 66 170 2020-09-10T21:09:15Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.9 0 67 171 2020-09-10T21:09:29Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.11 0 68 172 2020-09-10T21:09:41Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.13 0 69 174 2020-09-10T21:10:53Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.15 0 70 175 2020-09-10T21:11:03Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.17 0 71 176 2020-09-10T21:11:13Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.21 0 72 177 2020-09-10T21:11:24Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 12.19 0 73 178 2020-09-10T21:11:34Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 Chapter 11 0 14 180 121 2020-09-10T21:32:23Z Algowikiadmin 1 /* Transformations and Satisfiability */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10 :[[11.11]] :11.12 :[[11.13]] :11.14 :[[11.15]] :11.16 :[[11.17]] :11.18 :[[11.19]] :11.20 :[[11.21]] ===Creatvie Reductions=== :11.22 :[[11.23]] :11.24 :[[11.25]] :11.26 :[[11.27]] :11.28 :[[11.29]] :11.30 ===Algorithms for Special Cases=== :[[11.31]] :11.32 :[[11.33]] :11.34 :[[11.35]] Back to [[Chapter List]] dc960a84d02bd8f7a57513e6e478d6a4f554d654 187 180 2020-09-10T21:41:26Z Algowikiadmin 1 /* Basic Reductions */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]] :11.14 :[[11.15]] :11.16 :[[11.17]] :11.18 :[[11.19]] :11.20 :[[11.21]] ===Creatvie Reductions=== :11.22 :[[11.23]] :11.24 :[[11.25]] :11.26 :[[11.27]] :11.28 :[[11.29]] :11.30 ===Algorithms for Special Cases=== :[[11.31]] :11.32 :[[11.33]] :11.34 :[[11.35]] Back to [[Chapter List]] 5f5ae3a9243a5c2f525e3ecc76e0178f97c24044 189 187 2020-09-11T18:55:17Z Algowikiadmin 1 /* Basic Reductions */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]].In the minimum element set cover problem, we seek a set cover <math>S \subseteq C</math> of a universal set <math>U = {1, . . . , n}</math> such that sum of the sizes of the subsets in <math>S</math> is at most <math>k</math>. ::(a) Show that <math>C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}</math> has a cover of size 6, but none of size 5 because of a repeated element. ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.) [[11.13|Solution]] :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph <math>G</math> with <math>n</math> vertices, determine whether <math>G</math> has a simple cycle of length exactly <math>[n/2]</math>, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard. :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets <math>A</math>, <math>B</math>, or <math>C</math> such that <math>\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i</math>. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)). [[11.15|Solution]] :11.16. Show that the following problem is NP-complete: :* ''Problem:'' Dense subgraph :* ''Input:'' A graph <math>G</math>, and integers <math>k</math> and <math>y</math>. :* ''Output:'' Does <math>G</math> contain a subgraph with exactly <math>k</math> vertices and at least <math>y</math> edges? :[[11.17]]. Show that the following problem is NP-complete: :* ''Problem:'' Clique, no-clique :* ''Input:'' An undirected graph <math>G=(V,E)</math> and an integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain both a clique of size <math>k</math> and an independent set of size <math>k</math>. [[11.17|Solution]] :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.) :[[11.19]]. Show that the following problem is NP-hard: :*''Problem:'' Maximum Common Subgraph :*''Input:'' Two graphs <math>G_1 = (V_1, E_1)</math> and <math>G_2 = (V_2, E_2)</math>, and a budget <math>b</math>. :*Output: Two sets of nodes <math>S_1 \subseteq V_1</math> and <math>S_2 \subseteq V_2</math> whose deletion leaves at least <math>b</math> nodes in each graph, and makes the two graphs identical. [[11.19|Solution]] :11.20. A ''strongly independent'' set is a subset of vertices <math>S</math> in a graph <math>G</math> such that for any two vertices in <math>S</math>, there is no path of length two in <math>G</math>. Prove that strongly independent set is NP-hard. :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say <math>2n</math>, in which <math>n</math> of the vertices form a clique and the remaining <math>n</math> vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains <math>2g</math> nodes. Prove that ''max kite'' is NP-hard. [[11.21|Solution]] ===Creatvie Reductions=== :11.22 :[[11.23]] :11.24 :[[11.25]] :11.26 :[[11.27]] :11.28 :[[11.29]] :11.30 ===Algorithms for Special Cases=== :[[11.31]] :11.32 :[[11.33]] :11.34 :[[11.35]] Back to [[Chapter List]] 7e05a060f004193aa8e9a965a0ea4285ae2eebb7 190 189 2020-09-11T19:00:54Z Algowikiadmin 1 /* Creatvie Reductions */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]].In the minimum element set cover problem, we seek a set cover <math>S \subseteq C</math> of a universal set <math>U = {1, . . . , n}</math> such that sum of the sizes of the subsets in <math>S</math> is at most <math>k</math>. ::(a) Show that <math>C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}</math> has a cover of size 6, but none of size 5 because of a repeated element. ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.) [[11.13|Solution]] :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph <math>G</math> with <math>n</math> vertices, determine whether <math>G</math> has a simple cycle of length exactly <math>[n/2]</math>, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard. :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets <math>A</math>, <math>B</math>, or <math>C</math> such that <math>\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i</math>. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)). [[11.15|Solution]] :11.16. Show that the following problem is NP-complete: :* ''Problem:'' Dense subgraph :* ''Input:'' A graph <math>G</math>, and integers <math>k</math> and <math>y</math>. :* ''Output:'' Does <math>G</math> contain a subgraph with exactly <math>k</math> vertices and at least <math>y</math> edges? :[[11.17]]. Show that the following problem is NP-complete: :* ''Problem:'' Clique, no-clique :* ''Input:'' An undirected graph <math>G=(V,E)</math> and an integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain both a clique of size <math>k</math> and an independent set of size <math>k</math>. [[11.17|Solution]] :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.) :[[11.19]]. Show that the following problem is NP-hard: :*''Problem:'' Maximum Common Subgraph :*''Input:'' Two graphs <math>G_1 = (V_1, E_1)</math> and <math>G_2 = (V_2, E_2)</math>, and a budget <math>b</math>. :*Output: Two sets of nodes <math>S_1 \subseteq V_1</math> and <math>S_2 \subseteq V_2</math> whose deletion leaves at least <math>b</math> nodes in each graph, and makes the two graphs identical. [[11.19|Solution]] :11.20. A ''strongly independent'' set is a subset of vertices <math>S</math> in a graph <math>G</math> such that for any two vertices in <math>S</math>, there is no path of length two in <math>G</math>. Prove that strongly independent set is NP-hard. :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say <math>2n</math>, in which <math>n</math> of the vertices form a clique and the remaining <math>n</math> vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains <math>2g</math> nodes. Prove that ''max kite'' is NP-hard. [[11.21|Solution]] ===Creative Reductions=== :11.22. Prove that the following problem is NP-complete: :* ''Problem:'' Hitting Set :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain a subset <math>S'</math> such that <math>|S'| \leq k</math> and each subset in <math>C</math> contains at least one element from <math>S'</math>? :[[11.23]]. Prove that the following problem is NP-complete: :* ''Problem:'' Knapsack :* ''Input:'' A set <math>S</math> of <math>n</math> items, such that the <math>i</math>th item has value <math>v_i</math> and weight <math>w_i</math>. Two positive integers: weight limit <math>W</math> and value requirement <math>V</math>. :* ''Output:'' Does there exist a subset <math>S' \in S</math> such that <math>\sum_{i \in S'} w_i \leq W</math> and <math>\sum_{i \in S'} v_i \geq V</math>? :(Hint: start from integer partition.) [[11.23|Solution]] :11.24. Prove that the following problem is NP-complete: :* ''Problem:'' Hamiltonian Path :* ''Input:'' A graph <math>G</math>, and vertices <math>s</math> and <math>t</math>. :* ''Output:'' Does <math>G</math> contain a path which starts from <math>s</math>, ends at <math>t</math>, and visits all vertices without visiting any vertex more than once? (Hint: start from Hamiltonian cycle.) :[[11.25]]. Prove that the following problem is NP-complete: :* ''Problem:'' Longest Path :* ''Input:'' A graph <math>G</math> and positive integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain a path that visits at least <math>k</math> different vertices without visiting any vertex more than once? [[11.25|Solution]] :11.26. Prove that the following problem is NP-complete: :* ''Problem:'' Dominating Set :* ''Input:'' A graph <math>G=(V,E)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math> where for each vertex <math>x \in V</math> either <math>x \in V'</math> or there exists an edge <math>(x,y)</math>, where <math>y \in V'</math>. :[[11.27]]. Prove that the vertex cover problem (does there exist a subset <math>S</math> of <math>k</math> vertices in a graph <math>G</math> such that every edge in <math>G</math> is incident upon at least one vertex in <math>S</math>?) remains NP-complete even when all the vertices in the graph are restricted to have even degrees. [[11.27|Solution]] :11.28. Prove that the following problem is NP-complete: :* ''Problem:'' Set Packing :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain at least <math>k</math> disjoint subsets (i.e., such that none of these subsets have any elements in common?) :[[11.29]]. Prove that the following problem is NP-complete: :* ''Problem:'' Feedback Vertex Set :* ''Input:'' A directed graph <math>G=(V,A)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math>, such that deleting the vertices of <math>V'</math> from <math>G</math> leaves a DAG? [[11.29|Solution]] :11.30. Give a reduction from Sudoku to the vertex coloring problem in graphs. Specifically, describe how to take any partially filled Sudoku board and construct a graph that can be colored with nine colors iff the Sudoku board is solvable. ===Algorithms for Special Cases=== :[[11.31]] :11.32 :[[11.33]] :11.34 :[[11.35]] Back to [[Chapter List]] c48993aae6598a79803eab5ed7e15f2ec3b13d89 191 190 2020-09-11T19:12:03Z Algowikiadmin 1 /* Algorithms for Special Cases */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]].In the minimum element set cover problem, we seek a set cover <math>S \subseteq C</math> of a universal set <math>U = {1, . . . , n}</math> such that sum of the sizes of the subsets in <math>S</math> is at most <math>k</math>. ::(a) Show that <math>C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}</math> has a cover of size 6, but none of size 5 because of a repeated element. ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.) [[11.13|Solution]] :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph <math>G</math> with <math>n</math> vertices, determine whether <math>G</math> has a simple cycle of length exactly <math>[n/2]</math>, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard. :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets <math>A</math>, <math>B</math>, or <math>C</math> such that <math>\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i</math>. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)). [[11.15|Solution]] :11.16. Show that the following problem is NP-complete: :* ''Problem:'' Dense subgraph :* ''Input:'' A graph <math>G</math>, and integers <math>k</math> and <math>y</math>. :* ''Output:'' Does <math>G</math> contain a subgraph with exactly <math>k</math> vertices and at least <math>y</math> edges? :[[11.17]]. Show that the following problem is NP-complete: :* ''Problem:'' Clique, no-clique :* ''Input:'' An undirected graph <math>G=(V,E)</math> and an integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain both a clique of size <math>k</math> and an independent set of size <math>k</math>. [[11.17|Solution]] :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.) :[[11.19]]. Show that the following problem is NP-hard: :*''Problem:'' Maximum Common Subgraph :*''Input:'' Two graphs <math>G_1 = (V_1, E_1)</math> and <math>G_2 = (V_2, E_2)</math>, and a budget <math>b</math>. :*Output: Two sets of nodes <math>S_1 \subseteq V_1</math> and <math>S_2 \subseteq V_2</math> whose deletion leaves at least <math>b</math> nodes in each graph, and makes the two graphs identical. [[11.19|Solution]] :11.20. A ''strongly independent'' set is a subset of vertices <math>S</math> in a graph <math>G</math> such that for any two vertices in <math>S</math>, there is no path of length two in <math>G</math>. Prove that strongly independent set is NP-hard. :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say <math>2n</math>, in which <math>n</math> of the vertices form a clique and the remaining <math>n</math> vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains <math>2g</math> nodes. Prove that ''max kite'' is NP-hard. [[11.21|Solution]] ===Creative Reductions=== :11.22. Prove that the following problem is NP-complete: :* ''Problem:'' Hitting Set :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain a subset <math>S'</math> such that <math>|S'| \leq k</math> and each subset in <math>C</math> contains at least one element from <math>S'</math>? :[[11.23]]. Prove that the following problem is NP-complete: :* ''Problem:'' Knapsack :* ''Input:'' A set <math>S</math> of <math>n</math> items, such that the <math>i</math>th item has value <math>v_i</math> and weight <math>w_i</math>. Two positive integers: weight limit <math>W</math> and value requirement <math>V</math>. :* ''Output:'' Does there exist a subset <math>S' \in S</math> such that <math>\sum_{i \in S'} w_i \leq W</math> and <math>\sum_{i \in S'} v_i \geq V</math>? :(Hint: start from integer partition.) [[11.23|Solution]] :11.24. Prove that the following problem is NP-complete: :* ''Problem:'' Hamiltonian Path :* ''Input:'' A graph <math>G</math>, and vertices <math>s</math> and <math>t</math>. :* ''Output:'' Does <math>G</math> contain a path which starts from <math>s</math>, ends at <math>t</math>, and visits all vertices without visiting any vertex more than once? (Hint: start from Hamiltonian cycle.) :[[11.25]]. Prove that the following problem is NP-complete: :* ''Problem:'' Longest Path :* ''Input:'' A graph <math>G</math> and positive integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain a path that visits at least <math>k</math> different vertices without visiting any vertex more than once? [[11.25|Solution]] :11.26. Prove that the following problem is NP-complete: :* ''Problem:'' Dominating Set :* ''Input:'' A graph <math>G=(V,E)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math> where for each vertex <math>x \in V</math> either <math>x \in V'</math> or there exists an edge <math>(x,y)</math>, where <math>y \in V'</math>. :[[11.27]]. Prove that the vertex cover problem (does there exist a subset <math>S</math> of <math>k</math> vertices in a graph <math>G</math> such that every edge in <math>G</math> is incident upon at least one vertex in <math>S</math>?) remains NP-complete even when all the vertices in the graph are restricted to have even degrees. [[11.27|Solution]] :11.28. Prove that the following problem is NP-complete: :* ''Problem:'' Set Packing :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain at least <math>k</math> disjoint subsets (i.e., such that none of these subsets have any elements in common?) :[[11.29]]. Prove that the following problem is NP-complete: :* ''Problem:'' Feedback Vertex Set :* ''Input:'' A directed graph <math>G=(V,A)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math>, such that deleting the vertices of <math>V'</math> from <math>G</math> leaves a DAG? [[11.29|Solution]] :11.30. Give a reduction from Sudoku to the vertex coloring problem in graphs. Specifically, describe how to take any partially filled Sudoku board and construct a graph that can be colored with nine colors iff the Sudoku board is solvable. ===Algorithms for Special Cases=== :[[11.31]]. A Hamiltonian path <math>P</math> is a path that visits each vertex exactly once. The problem of testing whether a graph <math>G</math> contains a Hamiltonian path is NP-complete. There does not have to be an edge in <math>G</math> from the ending vertex to the starting vertex of <math>P</math>, unlike in the Hamiltonian cycle problem. Give an <math>O(n+m)</math>-time algorithm to test whether a directed acyclic graph <math>G</math> (a DAG) contains a Hamiltonian path. (Hint: think about topological sorting and DFS.) [[11.31|Solution]] :11.32. Consider the ''k''-clique problem, which is the general clique problem restricted to graphs in which every vertex has degree at most <math>k</math>. Prove that ''k''-clique has an efficient algorithm for any given <math>k</math>, meaning that <math>k</math> is a constant. :[[11.33]]. The <math>2</math>-SAT problem is, given a Boolean formula in 2-conjunctive normal form (CNF), to decide whether the formula is satisfiable. <math>2</math>-SAT is like <math>3</math>-SAT, except that each clause can have only two literals. For example, the following formula is in <math>2</math>-CNF: <math> (x_1 \or x_2) \and (\bar{x}_2 \or x_3) \and (x_1 \or \bar{x}_3) </math> :Give a polynomial-time algorithm to solve <math>2</math>-SAT. [[11.33|Solution]] :11.34, Show that the following problems are in NP: #Does graph <math>G</math> have a simple path (i.e., with no vertex repeated) of length <math>k</math>? \#Is integer <math>n</math> composite (i.e., not prime)? #Does graph <math>G</math> have a vertex cover of size <math>k</math>? :[[11.35]]. Until 2002, it was an open question whether the decision problem ''Is integer <math>n</math> a composite number, in other words, not prime?'' can be computed in time polynomial in the size of its input. Why doesn't the following algorithm suffice to prove it is in P, since it runs in <math>O(n)</math> time? PrimalityTesting(<math>n</math>) composite := <math>false</math> for i := 2 to <math>n-1</math> do if <math>(n\,\bmod\,i) = 0</math> then composite := <math>true</math> [[11.35|Solution]] Back to [[Chapter List]] e80be43b6429c4601dd5383cbaed091c36c5c21e 192 191 2020-09-11T19:13:39Z Algowikiadmin 1 /* Basic Reductions */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. [[11.11|Solution]] :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]].In the minimum element set cover problem, we seek a set cover <math>S \subseteq C</math> of a universal set <math>U = {1, . . . , n}</math> such that sum of the sizes of the subsets in <math>S</math> is at most <math>k</math>. ::(a) Show that <math>C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}</math> has a cover of size 6, but none of size 5 because of a repeated element. ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.) [[11.13|Solution]] :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph <math>G</math> with <math>n</math> vertices, determine whether <math>G</math> has a simple cycle of length exactly <math>[n/2]</math>, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard. :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets <math>A</math>, <math>B</math>, or <math>C</math> such that <math>\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i</math>. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)). [[11.15|Solution]] :11.16. Show that the following problem is NP-complete: :* ''Problem:'' Dense subgraph :* ''Input:'' A graph <math>G</math>, and integers <math>k</math> and <math>y</math>. :* ''Output:'' Does <math>G</math> contain a subgraph with exactly <math>k</math> vertices and at least <math>y</math> edges? :[[11.17]]. Show that the following problem is NP-complete: :* ''Problem:'' Clique, no-clique :* ''Input:'' An undirected graph <math>G=(V,E)</math> and an integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain both a clique of size <math>k</math> and an independent set of size <math>k</math>. [[11.17|Solution]] :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.) :[[11.19]]. Show that the following problem is NP-hard: :*''Problem:'' Maximum Common Subgraph :*''Input:'' Two graphs <math>G_1 = (V_1, E_1)</math> and <math>G_2 = (V_2, E_2)</math>, and a budget <math>b</math>. :*Output: Two sets of nodes <math>S_1 \subseteq V_1</math> and <math>S_2 \subseteq V_2</math> whose deletion leaves at least <math>b</math> nodes in each graph, and makes the two graphs identical. [[11.19|Solution]] :11.20. A ''strongly independent'' set is a subset of vertices <math>S</math> in a graph <math>G</math> such that for any two vertices in <math>S</math>, there is no path of length two in <math>G</math>. Prove that strongly independent set is NP-hard. :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say <math>2n</math>, in which <math>n</math> of the vertices form a clique and the remaining <math>n</math> vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains <math>2g</math> nodes. Prove that ''max kite'' is NP-hard. [[11.21|Solution]] ===Creative Reductions=== :11.22. Prove that the following problem is NP-complete: :* ''Problem:'' Hitting Set :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain a subset <math>S'</math> such that <math>|S'| \leq k</math> and each subset in <math>C</math> contains at least one element from <math>S'</math>? :[[11.23]]. Prove that the following problem is NP-complete: :* ''Problem:'' Knapsack :* ''Input:'' A set <math>S</math> of <math>n</math> items, such that the <math>i</math>th item has value <math>v_i</math> and weight <math>w_i</math>. Two positive integers: weight limit <math>W</math> and value requirement <math>V</math>. :* ''Output:'' Does there exist a subset <math>S' \in S</math> such that <math>\sum_{i \in S'} w_i \leq W</math> and <math>\sum_{i \in S'} v_i \geq V</math>? :(Hint: start from integer partition.) [[11.23|Solution]] :11.24. Prove that the following problem is NP-complete: :* ''Problem:'' Hamiltonian Path :* ''Input:'' A graph <math>G</math>, and vertices <math>s</math> and <math>t</math>. :* ''Output:'' Does <math>G</math> contain a path which starts from <math>s</math>, ends at <math>t</math>, and visits all vertices without visiting any vertex more than once? (Hint: start from Hamiltonian cycle.) :[[11.25]]. Prove that the following problem is NP-complete: :* ''Problem:'' Longest Path :* ''Input:'' A graph <math>G</math> and positive integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain a path that visits at least <math>k</math> different vertices without visiting any vertex more than once? [[11.25|Solution]] :11.26. Prove that the following problem is NP-complete: :* ''Problem:'' Dominating Set :* ''Input:'' A graph <math>G=(V,E)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math> where for each vertex <math>x \in V</math> either <math>x \in V'</math> or there exists an edge <math>(x,y)</math>, where <math>y \in V'</math>. :[[11.27]]. Prove that the vertex cover problem (does there exist a subset <math>S</math> of <math>k</math> vertices in a graph <math>G</math> such that every edge in <math>G</math> is incident upon at least one vertex in <math>S</math>?) remains NP-complete even when all the vertices in the graph are restricted to have even degrees. [[11.27|Solution]] :11.28. Prove that the following problem is NP-complete: :* ''Problem:'' Set Packing :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain at least <math>k</math> disjoint subsets (i.e., such that none of these subsets have any elements in common?) :[[11.29]]. Prove that the following problem is NP-complete: :* ''Problem:'' Feedback Vertex Set :* ''Input:'' A directed graph <math>G=(V,A)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math>, such that deleting the vertices of <math>V'</math> from <math>G</math> leaves a DAG? [[11.29|Solution]] :11.30. Give a reduction from Sudoku to the vertex coloring problem in graphs. Specifically, describe how to take any partially filled Sudoku board and construct a graph that can be colored with nine colors iff the Sudoku board is solvable. ===Algorithms for Special Cases=== :[[11.31]]. A Hamiltonian path <math>P</math> is a path that visits each vertex exactly once. The problem of testing whether a graph <math>G</math> contains a Hamiltonian path is NP-complete. There does not have to be an edge in <math>G</math> from the ending vertex to the starting vertex of <math>P</math>, unlike in the Hamiltonian cycle problem. Give an <math>O(n+m)</math>-time algorithm to test whether a directed acyclic graph <math>G</math> (a DAG) contains a Hamiltonian path. (Hint: think about topological sorting and DFS.) [[11.31|Solution]] :11.32. Consider the ''k''-clique problem, which is the general clique problem restricted to graphs in which every vertex has degree at most <math>k</math>. Prove that ''k''-clique has an efficient algorithm for any given <math>k</math>, meaning that <math>k</math> is a constant. :[[11.33]]. The <math>2</math>-SAT problem is, given a Boolean formula in 2-conjunctive normal form (CNF), to decide whether the formula is satisfiable. <math>2</math>-SAT is like <math>3</math>-SAT, except that each clause can have only two literals. For example, the following formula is in <math>2</math>-CNF: <math> (x_1 \or x_2) \and (\bar{x}_2 \or x_3) \and (x_1 \or \bar{x}_3) </math> :Give a polynomial-time algorithm to solve <math>2</math>-SAT. [[11.33|Solution]] :11.34, Show that the following problems are in NP: #Does graph <math>G</math> have a simple path (i.e., with no vertex repeated) of length <math>k</math>? \#Is integer <math>n</math> composite (i.e., not prime)? #Does graph <math>G</math> have a vertex cover of size <math>k</math>? :[[11.35]]. Until 2002, it was an open question whether the decision problem ''Is integer <math>n</math> a composite number, in other words, not prime?'' can be computed in time polynomial in the size of its input. Why doesn't the following algorithm suffice to prove it is in P, since it runs in <math>O(n)</math> time? PrimalityTesting(<math>n</math>) composite := <math>false</math> for i := 2 to <math>n-1</math> do if <math>(n\,\bmod\,i) = 0</math> then composite := <math>true</math> [[11.35|Solution]] Back to [[Chapter List]] 5d038ab4118c5a4ae689e7aa1fdd4f8d92545b10 11.1 0 74 181 2020-09-10T21:34:03Z Algowikiadmin 1 Created page with " Back to [[Chapter 12]]" wikitext text/x-wiki Back to [[Chapter 12]] 37dcc51bcfe2e9ceae6ae484deeb5fd397dd0bf3 182 181 2020-09-10T21:34:19Z Algowikiadmin 1 wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.3 0 75 183 2020-09-10T21:34:39Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.5 0 76 184 2020-09-10T21:35:04Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.7 0 77 185 2020-09-10T21:35:32Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.9 0 78 186 2020-09-10T21:35:41Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.11 0 79 188 2020-09-10T21:45:19Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.15 0 80 193 2020-09-11T19:16:53Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.17 0 81 194 2020-09-11T19:18:04Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.19 0 82 195 2020-09-11T19:18:31Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.13 0 83 196 2020-09-11T19:18:55Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.21 0 84 197 2020-09-11T19:21:09Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.23 0 85 198 2020-09-11T19:22:32Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.25 0 86 199 2020-09-11T19:23:53Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.27 0 87 200 2020-09-11T22:06:11Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.29 0 88 201 2020-09-11T22:06:30Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.31 0 89 202 2020-09-11T22:06:55Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 11.33 0 90 203 2020-09-11T22:07:35Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 Chapter 11 0 14 204 192 2020-09-11T22:08:11Z Algowikiadmin 1 /* Algorithms for Special Cases */ wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. [[11.11|Solution]] :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]].In the minimum element set cover problem, we seek a set cover <math>S \subseteq C</math> of a universal set <math>U = {1, . . . , n}</math> such that sum of the sizes of the subsets in <math>S</math> is at most <math>k</math>. ::(a) Show that <math>C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}</math> has a cover of size 6, but none of size 5 because of a repeated element. ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.) [[11.13|Solution]] :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph <math>G</math> with <math>n</math> vertices, determine whether <math>G</math> has a simple cycle of length exactly <math>[n/2]</math>, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard. :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets <math>A</math>, <math>B</math>, or <math>C</math> such that <math>\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i</math>. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)). [[11.15|Solution]] :11.16. Show that the following problem is NP-complete: :* ''Problem:'' Dense subgraph :* ''Input:'' A graph <math>G</math>, and integers <math>k</math> and <math>y</math>. :* ''Output:'' Does <math>G</math> contain a subgraph with exactly <math>k</math> vertices and at least <math>y</math> edges? :[[11.17]]. Show that the following problem is NP-complete: :* ''Problem:'' Clique, no-clique :* ''Input:'' An undirected graph <math>G=(V,E)</math> and an integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain both a clique of size <math>k</math> and an independent set of size <math>k</math>. [[11.17|Solution]] :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.) :[[11.19]]. Show that the following problem is NP-hard: :*''Problem:'' Maximum Common Subgraph :*''Input:'' Two graphs <math>G_1 = (V_1, E_1)</math> and <math>G_2 = (V_2, E_2)</math>, and a budget <math>b</math>. :*Output: Two sets of nodes <math>S_1 \subseteq V_1</math> and <math>S_2 \subseteq V_2</math> whose deletion leaves at least <math>b</math> nodes in each graph, and makes the two graphs identical. [[11.19|Solution]] :11.20. A ''strongly independent'' set is a subset of vertices <math>S</math> in a graph <math>G</math> such that for any two vertices in <math>S</math>, there is no path of length two in <math>G</math>. Prove that strongly independent set is NP-hard. :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say <math>2n</math>, in which <math>n</math> of the vertices form a clique and the remaining <math>n</math> vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains <math>2g</math> nodes. Prove that ''max kite'' is NP-hard. [[11.21|Solution]] ===Creative Reductions=== :11.22. Prove that the following problem is NP-complete: :* ''Problem:'' Hitting Set :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain a subset <math>S'</math> such that <math>|S'| \leq k</math> and each subset in <math>C</math> contains at least one element from <math>S'</math>? :[[11.23]]. Prove that the following problem is NP-complete: :* ''Problem:'' Knapsack :* ''Input:'' A set <math>S</math> of <math>n</math> items, such that the <math>i</math>th item has value <math>v_i</math> and weight <math>w_i</math>. Two positive integers: weight limit <math>W</math> and value requirement <math>V</math>. :* ''Output:'' Does there exist a subset <math>S' \in S</math> such that <math>\sum_{i \in S'} w_i \leq W</math> and <math>\sum_{i \in S'} v_i \geq V</math>? :(Hint: start from integer partition.) [[11.23|Solution]] :11.24. Prove that the following problem is NP-complete: :* ''Problem:'' Hamiltonian Path :* ''Input:'' A graph <math>G</math>, and vertices <math>s</math> and <math>t</math>. :* ''Output:'' Does <math>G</math> contain a path which starts from <math>s</math>, ends at <math>t</math>, and visits all vertices without visiting any vertex more than once? (Hint: start from Hamiltonian cycle.) :[[11.25]]. Prove that the following problem is NP-complete: :* ''Problem:'' Longest Path :* ''Input:'' A graph <math>G</math> and positive integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain a path that visits at least <math>k</math> different vertices without visiting any vertex more than once? [[11.25|Solution]] :11.26. Prove that the following problem is NP-complete: :* ''Problem:'' Dominating Set :* ''Input:'' A graph <math>G=(V,E)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math> where for each vertex <math>x \in V</math> either <math>x \in V'</math> or there exists an edge <math>(x,y)</math>, where <math>y \in V'</math>. :[[11.27]]. Prove that the vertex cover problem (does there exist a subset <math>S</math> of <math>k</math> vertices in a graph <math>G</math> such that every edge in <math>G</math> is incident upon at least one vertex in <math>S</math>?) remains NP-complete even when all the vertices in the graph are restricted to have even degrees. [[11.27|Solution]] :11.28. Prove that the following problem is NP-complete: :* ''Problem:'' Set Packing :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain at least <math>k</math> disjoint subsets (i.e., such that none of these subsets have any elements in common?) :[[11.29]]. Prove that the following problem is NP-complete: :* ''Problem:'' Feedback Vertex Set :* ''Input:'' A directed graph <math>G=(V,A)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math>, such that deleting the vertices of <math>V'</math> from <math>G</math> leaves a DAG? [[11.29|Solution]] :11.30. Give a reduction from Sudoku to the vertex coloring problem in graphs. Specifically, describe how to take any partially filled Sudoku board and construct a graph that can be colored with nine colors iff the Sudoku board is solvable. ===Algorithms for Special Cases=== :[[11.31]]. A Hamiltonian path <math>P</math> is a path that visits each vertex exactly once. The problem of testing whether a graph <math>G</math> contains a Hamiltonian path is NP-complete. There does not have to be an edge in <math>G</math> from the ending vertex to the starting vertex of <math>P</math>, unlike in the Hamiltonian cycle problem. Give an <math>O(n+m)</math>-time algorithm to test whether a directed acyclic graph <math>G</math> (a DAG) contains a Hamiltonian path. (Hint: think about topological sorting and DFS.) [[11.31|Solution]] :11.32. Consider the ''k''-clique problem, which is the general clique problem restricted to graphs in which every vertex has degree at most <math>k</math>. Prove that ''k''-clique has an efficient algorithm for any given <math>k</math>, meaning that <math>k</math> is a constant. :[[11.33]]. The <math>2</math>-SAT problem is, given a Boolean formula in 2-conjunctive normal form (CNF), to decide whether the formula is satisfiable. <math>2</math>-SAT is like <math>3</math>-SAT, except that each clause can have only two literals. For example, the following formula is in <math>2</math>-CNF: <math> (x_1 \or x_2) \and (\bar{x}_2 \or x_3) \and (x_1 \or \bar{x}_3) </math> :Give a polynomial-time algorithm to solve <math>2</math>-SAT. [[11.33|Solution]] :11.34, Show that the following problems are in NP: #Does graph <math>G</math> have a simple path (i.e., with no vertex repeated) of length <math>k</math>? \#Is integer <math>n</math> composite (i.e., not prime)? #Does graph <math>G</math> have a vertex cover of size <math>k</math>? :[[11.35]]. Until 2002, it was an open question whether the decision problem ''Is integer <math>n</math> a composite number, in other words, not prime?'' can be computed in time polynomial in the size of its input. Why doesn't the following algorithm suffice to prove it is in P, since it runs in <math>O(n)</math> time? PrimalityTesting(<math>n</math>) composite := <math>false</math> for i := 2 to <math>n-1</math> do if <math>(n\,\bmod\,i) = 0</math> then composite := <math>true</math> [[11.35|Solution]] Back to [[Chapter List]] 14c9bd71fc58f891913e1d6ec63d0417d87f5fc4 11.35 0 91 205 2020-09-11T22:09:27Z Algowikiadmin 1 Created page with " Back to [[Chapter 11]]" wikitext text/x-wiki Back to [[Chapter 11]] e6667b781e75470a0341dedfd35467b45138fdc2 Chapter 10 0 13 206 120 2020-09-11T22:17:24Z Algowikiadmin 1 /* Elementary Recurrences */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 ≤ i ≤ n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s × t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? (b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? ===Edit Distance=== :10.6 :[[10.7]] :10.8 :[[10.9]] :10.10 ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 97ef44a3d095cd575de40591f75b3f22fbfd9ce6 207 206 2020-09-11T22:21:26Z Algowikiadmin 1 /* Dynamic Programming */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 ≤ i ≤ n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s × t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? ===Edit Distance=== :10.6 :[[10.7]] :10.8 :[[10.9]] :10.10 ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] ab1e377c538be1dcdc7200352151885caa3043f9 208 207 2020-09-11T22:22:58Z Algowikiadmin 1 /* Elementary Recurrences */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s × t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? ===Edit Distance=== :10.6 :[[10.7]] :10.8 :[[10.9]] :10.10 ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 60dbb57fe24933c3fb185aa105842d74531725a1 209 208 2020-09-11T22:23:21Z Algowikiadmin 1 /* Elementary Recurrences */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? ===Edit Distance=== :10.6 :[[10.7]] :10.8 :[[10.9]] :10.10 ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 6628ebf2956bc2d687612f1da78ec869e5869e58 210 209 2020-09-13T00:07:48Z Algowikiadmin 1 /* Elementary Recurrences */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6 :[[10.7]] :10.8 :[[10.9]] :10.10 ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 991d9a50a7358cae80c303cc8fa058d5e9750e80 211 210 2020-09-13T19:25:20Z Algowikiadmin 1 /* Edit Distance */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. (b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| − |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9||Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 81699e40b198324afb63ff196418cd095b975be3 212 211 2020-09-13T19:28:05Z Algowikiadmin 1 /* Edit Distance */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]] :10.16 :[[10.17]] :10.18 :[[10.19]] :10.20 :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] b9a19fbe22941d6ca77203023d0ed519f216a20f 213 212 2020-09-13T19:44:07Z Algowikiadmin 1 /* Number Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]] :10.12 :[[10.13]] :10.14 ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] daf8591b814e894d42f562fed2e3f634c0212dca 214 213 2020-09-13T19:54:32Z Algowikiadmin 1 /* Greedy Algorithms */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]] :10.22 :[[10.23]] :10.24 :[[10.25]] :10.26 ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] c071dbb24cbc32143c40991f5020b41f7b3a5bb3 215 214 2020-09-13T20:18:37Z Algowikiadmin 1 /* Number Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]] :10.28 :[[10.29]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] d16a4204bbdda4b4ca10626d7019c53836511bd2 216 215 2020-09-13T20:26:41Z Algowikiadmin 1 /* Graphing Problem */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30 :[[10.31]] :10.32 :[[10.33]] :10.34 :[[10.35]] :10.36 :[[10.37]] :10.38 ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 1b48a61b7efd60a4cb2c15ab4548eee71546dfa9 217 216 2020-09-13T20:43:12Z Algowikiadmin 1 /* Design Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w)</math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 55cdaad5441d022b8f71b6af8dfb48da68196f86 218 217 2020-09-13T20:43:50Z Algowikiadmin 1 /* Design Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 9c8b5c51b2deb6dee40886e3e68f9d0d5e3dc6ea 219 218 2020-09-13T20:44:41Z Algowikiadmin 1 /* Design Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w)</math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] a4a9de88f2976cd4f5ce46901e577c9d31c73adb 220 219 2020-09-13T20:44:47Z Algowikiadmin 1 /* Design Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neigh- boring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 9c8b5c51b2deb6dee40886e3e68f9d0d5e3dc6ea 221 220 2020-09-13T20:45:19Z Algowikiadmin 1 /* Edit Distance */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]] :10.40 :[[10.41]] Back to [[Chapter List]] 352d5010d4c74aa3435ff344550ae09128ff7f84 222 221 2020-09-13T20:48:32Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. Back to [[Chapter List]] 14efa4988e8804b68f6b427327313c5fb1a80737 223 222 2020-09-13T20:48:56Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array} </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] afce26114a5a179e1308ffbd3d800431a3787ff9 228 223 2020-09-13T21:37:51Z Algowikiadmin 1 /* Number Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center><math> \begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array}</math> </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] 66bf8e39b603552ec7f191c8b3ab4b01d56ed37f Chapter 9 0 12 224 119 2020-09-13T21:08:55Z Algowikiadmin 1 /* Permutations */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6 :[[9.7]] :9.8 :[[9.9]] :9.10 :[[9.11]] :9.12 :[[9.13]] :9.14 :[[9.15]] ===Games and Puzzles=== :9.16 :[[9.17]] :9.18 :[[9.19]] :9.20 ===Combinatorial Optimization=== :[[9.21]] :9.22 :[[9.23]] :9.24 :[[9.25]] :9.26 :[[9.27]] ===Interview Problems=== :9.28 :[[9.27]] :9.28 :[[9.29]] :9.30 :[[9.31]] :9.32 :[[9.33]] Back to [[Chapter List]] 90a9c121d9d269a2f310612efe4965f964a4ea7c 225 224 2020-09-13T21:26:47Z Algowikiadmin 1 /* Backtracking */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16 :[[9.17]] :9.18 :[[9.19]] :9.20 ===Combinatorial Optimization=== :[[9.21]] :9.22 :[[9.23]] :9.24 :[[9.25]] :9.26 :[[9.27]] ===Interview Problems=== :9.28 :[[9.27]] :9.28 :[[9.29]] :9.30 :[[9.31]] :9.32 :[[9.33]] Back to [[Chapter List]] 63378d260738b0fc586d5d8905c9ba9f348e150e 226 225 2020-09-13T21:36:32Z Algowikiadmin 1 /* Games and Puzzles */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary. :[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once. [[9.17|Solution]] :9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board: <center><math>\begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix}</math></center> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>. :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are: <center><math>\begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} </math></center> [[9.19|Solution]] :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph. ===Combinatorial Optimization=== :[[9.21]] :9.22 :[[9.23]] :9.24 :[[9.25]] :9.26 :[[9.27]] ===Interview Problems=== :9.28 :[[9.27]] :9.28 :[[9.29]] :9.30 :[[9.31]] :9.32 :[[9.33]] Back to [[Chapter List]] 8bac79b44c1b397d895d5cf631231a9bfdf50734 227 226 2020-09-13T21:37:05Z Algowikiadmin 1 /* Permutations */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math> permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary. :[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once. [[9.17|Solution]] :9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board: <center><math>\begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix}</math></center> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>. :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are: <center><math>\begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} </math></center> [[9.19|Solution]] :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph. ===Combinatorial Optimization=== :[[9.21]] :9.22 :[[9.23]] :9.24 :[[9.25]] :9.26 :[[9.27]] ===Interview Problems=== :9.28 :[[9.27]] :9.28 :[[9.29]] :9.30 :[[9.31]] :9.32 :[[9.33]] Back to [[Chapter List]] 80c0d3b20424ca099705eeeb03c1dc7564ac6dc2 229 227 2020-09-13T21:40:49Z Algowikiadmin 1 /* Combinatorial Optimization */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math> permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary. :[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once. [[9.17|Solution]] :9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board: <center><math>\begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix}</math></center> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>. :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are: <center><math>\begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} </math></center> [[9.19|Solution]] :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph. ===Combinatorial Optimization=== For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice? :[[9.21]]. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470). [[9.21|Solution]] :9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537). :[[9.23]]. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586). [[9.23|Solution]] :9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604). :[[9.25]]. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608). [[9.25|Solution]] :9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618). :[[9.27]]. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678). [[9.27|Solution]] ===Interview Problems=== :9.28 :[[9.27]] :9.28 :[[9.29]] :9.30 :[[9.31]] :9.32 :[[9.33]] Back to [[Chapter List]] 6389908a7b2465a2f89a1c92e7b3b17a3ef01fdd 230 229 2020-09-13T21:59:21Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math> permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary. :[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once. [[9.17|Solution]] :9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board: <center><math>\begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix}</math></center> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>. :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are: <center><math>\begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} </math></center> [[9.19|Solution]] :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph. ===Combinatorial Optimization=== For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice? :[[9.21]]. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470). [[9.21|Solution]] :9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537). :[[9.23]]. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586). [[9.23|Solution]] :9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604). :[[9.25]]. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608). [[9.25|Solution]] :9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618). :[[9.27]]. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678). [[9.27|Solution]] ===Interview Problems=== :9.28. Write a function to find all permutations of the letters in a given string. :[[9.29]]. Implement an efficient algorithm for listing all <math>k</math>-element subsets of <math>n</math> items. [[9.29|Solution]] :9.30. An anagram is a rearrangement of the letters in a given string into a sequence of dictionary words, like ''Steven Skiena'' into ''Vainest Knees''. Propose an algorithm to construct all the anagrams of a given string. :[[9.31]]. Telephone keypads have letters on each numerical key. Write a program that generates all possible words resulting from translating a given digit sequence (e.g. 145345) into letters. [[9.31|Solution]] :9.32. You start with an empty room and a group of <math>n</math> people waiting outside. At each step, you may either admit one person into the room, or let one out. Can you arrange a sequence of <math>2n</math> steps, so that every possible combination of people is achieved exactly once? :[[9.33]]. Use a random number generator (rng04) that generates numbers from {0, 1, 2, 3, 4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability. What is the expected number of calls to rng04 per call of rng07? [[9.33|Solution]] Back to [[Chapter List]] 6a0327a54291a0682cc2820aee841779b0a81911 Chapter 8 0 11 231 118 2020-09-13T22:00:04Z Algowikiadmin 1 /* Weighted Graph Algorithms */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]] ===Minimum Spanning Tree=== :8.2 :[[8.3]] :8.4 :[[8.5]] :8.6 :[[8.7]] :8.8 :[[8.9]] :8.10 :[[8.11]] :8.12 ===Union Find=== :[[8.13]] :8.14 ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] 81d6ec0aa0bfaf0201b3d743e8d3013d614f9f84 232 231 2020-09-13T22:31:17Z Algowikiadmin 1 /* Simulating Graph Algorithms */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. ===Minimum Spanning Tree=== :8.2 :[[8.3]] :8.4 :[[8.5]] :8.6 :[[8.7]] :8.8 :[[8.9]] :8.10 :[[8.11]] :8.12 ===Union Find=== :[[8.13]] :8.14 ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] fef0a64094df7b4ce126117da037efcaffe7c399 233 232 2020-09-13T22:31:33Z Algowikiadmin 1 /* Simulating Graph Algorithms */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2 :[[8.3]] :8.4 :[[8.5]] :8.6 :[[8.7]] :8.8 :[[8.9]] :8.10 :[[8.11]] :8.12 ===Union Find=== :[[8.13]] :8.14 ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] 5d806278de86d21d7989cc58e122d3346112e7f3 234 233 2020-09-13T22:43:32Z Algowikiadmin 1 /* Minimum Spanning Tree */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]] :8.12 ===Union Find=== :[[8.13]] :8.14 ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] b140f5a86790ad069b60b007fda5b1adb0d8d150 235 234 2020-09-13T23:01:04Z Algowikiadmin 1 /* Minimum Spanning Tree */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]] :8.14 ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] e63c94da5b9a3d3f6beb661cd3ed2fcdef2e285f 236 235 2020-09-13T23:04:29Z Algowikiadmin 1 /* Union Find */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8:15]] :8.16 :[[8.17]] :8.18 :[[8.19]] :8.20 :[[8.21]] :8.22 :[[8.23]] :8.24 :[[8.25]] :8.26 :[[8.27]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] 66efde1ab30453cd11ba6d15ee0a34f57bedd68d 237 236 2020-09-13T23:31:33Z Algowikiadmin 1 /* Shortest Paths */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem. [[8.15|Solution]] :8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>? :[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>. :(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>. :(c)Can the two trees be completely disjointed? [[8.17|Solution]] :8.18. Either prove the following or give a counterexample: :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>. :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path. :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph. [[8.21|Solution]] :8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm? :[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time. [[8.23|Solution]] :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample. :[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>. [[8.25|Solution]] :8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple. :[[8.27]].''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path. [[8.27|Solution]] ===Network Flow and Matching=== :8.28 :[[8.29]] Back to [[Chapter List]] 1cfd51e29eb684fe73ccf118823d41ea2b6c39a2 238 237 2020-09-14T15:16:20Z Algowikiadmin 1 /* Network Flow and Matching */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem. [[8.15|Solution]] :8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>? :[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>. :(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>. :(c)Can the two trees be completely disjointed? [[8.17|Solution]] :8.18. Either prove the following or give a counterexample: :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>. :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path. :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph. [[8.21|Solution]] :8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm? :[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time. [[8.23|Solution]] :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample. :[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>. [[8.25|Solution]] :8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple. :[[8.27]].''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path. [[8.27|Solution]] ===Network Flow and Matching=== :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree. :[[8.29]]. An edge cover of an undirected graph <math>G = (V, E)</math> is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for <math>G</math>. [[8.29|Solution]] Back to [[Chapter List]] a81d8fdff720d730dbe2a0ca2da06c63c0d80b6f Chapter 7 0 10 239 117 2020-09-14T16:13:10Z Algowikiadmin 1 /* Simulating Graph Algorithms */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): <br>(see book for figure) <br>(see book for figure) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). (b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: <br>(see book for figure) ===Traversal=== :[[7.3]] :7.4 :[[7.5]] :7.6 :[[7.7]] :7.8 :[[7.9]] :7.10 :[[7.11]] :7.12 ===Applications=== :[[7.13]] :7.14 :[[7.15]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] 8a9c73751bbdb10a746ec7a5f8ce2e1122bf688b 240 239 2020-09-14T16:13:43Z Algowikiadmin 1 /* Simulating Graph Algorithms */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]] :7.4 :[[7.5]] :7.6 :[[7.7]] :7.8 :[[7.9]] :7.10 :[[7.11]] :7.12 ===Applications=== :[[7.13]] :7.14 :[[7.15]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] 8972791eb44652284b5390a0896d3fe095b4bb35 241 240 2020-09-14T16:25:02Z Algowikiadmin 1 /* Traversal */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]] :7.14 :[[7.15]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] af1edcf36501ec8d2baa45ec1007a54142dfa352 242 241 2020-09-14T16:31:00Z Algowikiadmin 1 /* Applications */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16 :[[7.17]] :7.18 :[[7.19]] :7.20 :[[7.21]]] :7.22 :[[7.23]] :7.24 :[[7.25]] :7.26 ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] 47fefd08e4ff067202e0d1361d6cafd8037b7ca8 243 242 2020-09-14T17:13:32Z Algowikiadmin 1 /* Algorithm Design */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]] :7.28 :[[7.29]] :7.30 :[[7.31]] :7.32 :[[7.33]] :7.34 :[[7.35]] :7.36 :[[7.37]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] 466682e7988effbab8b9da065acf18eb328f9995 244 243 2020-09-14T17:30:14Z Algowikiadmin 1 /* Directed Graphs */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38 :[[7.39]] :7.40 :[[7.41]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] ddaf53255bb3e309223897ca924df409a90aaf3d 245 244 2020-09-14T17:42:09Z Algowikiadmin 1 /* Articulation Vertices */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38. An articulation vertex of a connected graph <math>G<math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) [[7.39|Solution]] :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. [[7.41|Solution]] ===Interview Problems=== :7.42 :[[7.43]] Back to [[Chapter List]] d18d41d7d01902f61d30a703a6f9971b3205e9d6 246 245 2020-09-14T17:42:51Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38. An articulation vertex of a connected graph <math>G<math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) [[7.39|Solution]] :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. [[7.41|Solution]] ===Interview Problems=== :7.42. Which data structures are used in depth-first and breath-first search? :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order. [[7.43|Solution]] Back to [[Chapter List]] f7642312a303d803071867885f8106ecba31b93a 247 246 2020-09-14T17:43:14Z Algowikiadmin 1 /* Articulation Vertices */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38. An articulation vertex of a connected graph <math>G</math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) [[7.39|Solution]] :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. [[7.41|Solution]] ===Interview Problems=== :7.42. Which data structures are used in depth-first and breath-first search? :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order. [[7.43|Solution]] Back to [[Chapter List]] c47fb609947f40438bc92b5732e1737a1f3e0f34 Chapter 6 0 9 248 114 2020-09-14T17:53:02Z Algowikiadmin 1 /* Propability */ wikitext text/x-wiki =Hashing and Randomized Algorithms= ===Probability=== :[[6.1]]. You are given <math>n</math> unbiased coins, and perform the following process to generate all heads. Toss all <math>n</math> coins independently at random onto a table. Each round consists of picking up all the tails-up coins and tossing them onto the table again. You repeat until all coins are heads. :(a) What is the expected number of rounds performed by the process? :(b) What is the expected number of coin tosses performed by the process? [[6.1|Solution]] :6.2. Suppose we flip <math>n</math> coins each of known bias, such that <math>p_i</math> is the probability of the <math>i</math>th coin being a head. Present an efficient algorithm to determine the exact probability of getting exactly <math>k</math> heads given <math>p_1, . . . , p_n \in [0, 1]</math>. :[[6.3]]. An inversion of a permutation is a pair of elements that are out of order. :(a) Show that a permutation of <math>n</math> items has at most <math>n(n - 1)/2</math> inversions. Which permutation(s) have exactly n(n - 1)/2 inversions? :(b) Let P be a permutation and <math>P^r</math> be the reversal of this permutation. Show that <math>P</math> and <math>P^r</math> have a total of exactly <math>n(n - 1)/2</math> inversions. :(c) Use the previous result to argue that the expected number of inversions in a random permutation is <math>n(n - 1)/4</math>. [[6.3|Solution]] :6.4. A derangement is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i \neq i</math> for all <math>1 \leq i \leq n</math>. What is the probability that a random permutation is a derangement? ===Hashing=== :[[6.5]] :6.6 :[[6.7]] ===Randomized Algorithms=== :6.8 :[[6.9]] :6.10 :[[6.11]] :6.12 Back to [[Chapter List]] d6de7d0c9f987cbcf8c58b7422969254cddeb51d 249 248 2020-09-14T17:57:20Z Algowikiadmin 1 /* Hashing */ wikitext text/x-wiki =Hashing and Randomized Algorithms= ===Probability=== :[[6.1]]. You are given <math>n</math> unbiased coins, and perform the following process to generate all heads. Toss all <math>n</math> coins independently at random onto a table. Each round consists of picking up all the tails-up coins and tossing them onto the table again. You repeat until all coins are heads. :(a) What is the expected number of rounds performed by the process? :(b) What is the expected number of coin tosses performed by the process? [[6.1|Solution]] :6.2. Suppose we flip <math>n</math> coins each of known bias, such that <math>p_i</math> is the probability of the <math>i</math>th coin being a head. Present an efficient algorithm to determine the exact probability of getting exactly <math>k</math> heads given <math>p_1, . . . , p_n \in [0, 1]</math>. :[[6.3]]. An inversion of a permutation is a pair of elements that are out of order. :(a) Show that a permutation of <math>n</math> items has at most <math>n(n - 1)/2</math> inversions. Which permutation(s) have exactly n(n - 1)/2 inversions? :(b) Let P be a permutation and <math>P^r</math> be the reversal of this permutation. Show that <math>P</math> and <math>P^r</math> have a total of exactly <math>n(n - 1)/2</math> inversions. :(c) Use the previous result to argue that the expected number of inversions in a random permutation is <math>n(n - 1)/4</math>. [[6.3|Solution]] :6.4. A derangement is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i \neq i</math> for all <math>1 \leq i \leq n</math>. What is the probability that a random permutation is a derangement? ===Hashing=== :[[6.5]]. An all-Beatles radio station plays nothing but recordings by the Beatles, selecting the next song at random (uniformly with replacement). They get through about ten songs per hour. I listened for 90 minutes before hearing a repeated song. Estimate how many songs the Beatles recorded. [[6.5|Solution]] :6.6. Given strings <math>S</math> and <math>T</math> of length <math>n</math> and <math>m</math> respectively, find the shortest window in <math>S</math> that contains all the characters in <math>T</math> in expected <math>O(n + m)</math> time. :[[6.7]]. Design and implement an efficient data structure to maintain a ''least recently used'' (LRU) cache of <math>n</math> integer elements. A LRU cache will discard the least recently accessed element once the cache has reached its capacity, supporting the following operations: :• ''get(k)''– Return the value associated with the key <math>k</math> if it currently exists in the cache, otherwise return -1. :• ''put(k,v)'' – Set the value associated with key <math>k</math> to <math>v</math>, or insert if <math>k</math> is not already present. If there are already <math>n</math> items in the queue, delete the least recently used item before inserting <math>(k, v)</math>. Both operations should be done in <math>O(1)</math> expected time. [[6.7|Solution]] ===Randomized Algorithms=== :6.8 :[[6.9]] :6.10 :[[6.11]] :6.12 Back to [[Chapter List]] 2fb1d1536e7790e2cc659c40a44fad211215a6a7 250 249 2020-09-14T18:02:14Z Algowikiadmin 1 /* Randomized Algorithms */ wikitext text/x-wiki =Hashing and Randomized Algorithms= ===Probability=== :[[6.1]]. You are given <math>n</math> unbiased coins, and perform the following process to generate all heads. Toss all <math>n</math> coins independently at random onto a table. Each round consists of picking up all the tails-up coins and tossing them onto the table again. You repeat until all coins are heads. :(a) What is the expected number of rounds performed by the process? :(b) What is the expected number of coin tosses performed by the process? [[6.1|Solution]] :6.2. Suppose we flip <math>n</math> coins each of known bias, such that <math>p_i</math> is the probability of the <math>i</math>th coin being a head. Present an efficient algorithm to determine the exact probability of getting exactly <math>k</math> heads given <math>p_1, . . . , p_n \in [0, 1]</math>. :[[6.3]]. An inversion of a permutation is a pair of elements that are out of order. :(a) Show that a permutation of <math>n</math> items has at most <math>n(n - 1)/2</math> inversions. Which permutation(s) have exactly n(n - 1)/2 inversions? :(b) Let P be a permutation and <math>P^r</math> be the reversal of this permutation. Show that <math>P</math> and <math>P^r</math> have a total of exactly <math>n(n - 1)/2</math> inversions. :(c) Use the previous result to argue that the expected number of inversions in a random permutation is <math>n(n - 1)/4</math>. [[6.3|Solution]] :6.4. A derangement is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i \neq i</math> for all <math>1 \leq i \leq n</math>. What is the probability that a random permutation is a derangement? ===Hashing=== :[[6.5]]. An all-Beatles radio station plays nothing but recordings by the Beatles, selecting the next song at random (uniformly with replacement). They get through about ten songs per hour. I listened for 90 minutes before hearing a repeated song. Estimate how many songs the Beatles recorded. [[6.5|Solution]] :6.6. Given strings <math>S</math> and <math>T</math> of length <math>n</math> and <math>m</math> respectively, find the shortest window in <math>S</math> that contains all the characters in <math>T</math> in expected <math>O(n + m)</math> time. :[[6.7]]. Design and implement an efficient data structure to maintain a ''least recently used'' (LRU) cache of <math>n</math> integer elements. A LRU cache will discard the least recently accessed element once the cache has reached its capacity, supporting the following operations: :• ''get(k)''– Return the value associated with the key <math>k</math> if it currently exists in the cache, otherwise return -1. :• ''put(k,v)'' – Set the value associated with key <math>k</math> to <math>v</math>, or insert if <math>k</math> is not already present. If there are already <math>n</math> items in the queue, delete the least recently used item before inserting <math>(k, v)</math>. Both operations should be done in <math>O(1)</math> expected time. [[6.7|Solution]] ===Randomized Algorithms=== :6.8. A pair of English words <math>(w_1, w_2)</math> is called a ''rotodrome'' if one can be circularly shifted (rotated) to create the other word. For example, the words (windup, upwind) are a rotodrome pair, because we can rotate “windup” two positions to the right to get “upwind.” :Give an efficient algorithm to find all rotodrome pairs among <math>n</math> words of length <math>k</math>, with a worst-case analysis. Also give a faster expected-time algorithm based on hashing. :[[6.9]]. Given an array <math>w</math> of positive integers, where <math>w[i]</math> describes the weight of index <math>i</math>, propose an algorithm that randomly picks an index in proportion to its weight. [[6.9|Solution]] :6.10. You are given a function ''rand7'', which generates a uniform random integer in the range 1 to 7. Use it to produce a function ''rand10'', which generates a uniform random integer in the range 1 to 10. :[[6.11]]. Let <math>0 < \alpha < 1/2</math> be some constant, independent of the input array length <math>n</math>. What is the probability that, with a randomly chosen pivot element, the partition subroutine from quicksort produces a split in which the size of both the resulting subproblems is at least <math>\alpha</math> times the size of the original array? [[6.11|Solution]] :6.12. Show that for any given load <math>m/n</math>, the error probability of a Bloom filter is minimized when the number of hash functions is <math>k = exp(-1)/(m/n)</math>. Back to [[Chapter List]] 72b1caa31cac5e1dd82ce0b7a9b5c4dcff357f7c Chapter 5 0 8 251 113 2020-09-14T18:33:24Z Algowikiadmin 1 /* Binary Search */ wikitext text/x-wiki =Divide and Conquer= ===Binary Search=== :[[5.1]]. Suppose you are given a sorted array <math>A</math> of size n that has been circularly shifted <math>k</math> positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted <math>k = 2</math> positions, while [27, 29, 35, 42, 5, 15] has been shifted <math>k = 4</math> positions. :• Suppose you know what <math>k</math> is. Give an <math>O(1)</math> algorithm to find the largest number in <math>A</math>. :• Suppose you do not know what <math>k</math> is. Give an <math>O(lg n)</math> algorithm to find the largest number in <math>A</math>. For partial credit, you may give an <math>O(n)</math> algorithm. [[5.1|Solution]] :5.2. A sorted array of size n contains distinct integers between <math>1</math> and <math>n + 1</math> , with one element missing. Give an <math>O(log n)</math> algorithm to find the missing integer, without using any extra space. :[[5.3]] Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to <math>n</math>. The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats. :(a) What is an optimal strategy if <math>n</math> in known? :(b) What is a good strategy if <math>n</math> is not known? [[5.3|Solution]] :5.4. You are given a unimodal array of <math>n</math> distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in <math>O(log n)</math> time. :[[5.5]]. Suppose that you are given a sorted sequence of distinct integers <math>[a_1, a_2, . . . , a_n]</math>. Give an <math>O(lg n)</math> algorithm to determine whether there exists an index <math>i</math> such that <math>a_i = i</math>. For example, in [-10, -3, 3, 5, 7], <math>a_3 = 3</math>. In [2, 3, 4, 5, 6, 7], there is no such <math>i</math>. [[5.5|Solution]] :5.6. Suppose that you are given a sorted sequence of distinct integers <math>a = [a_1, a_2, . . . , a_n]</math>, drawn from 1 to <math>m</math> where <math>n < m</math>. Give an <math>O(lg n)</math> algorithm to find an integer <math>\leq m</math> that is not present in <math>a</math>. For full credit, find the smallest such integer <math>x</math> such that <math>1 \leq x \leq m</math>. :[[5.7]]. Let <math>M</math> be an <math>n * m</math> integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer <math>x</math> in <math>M</math>, or to determine that <math>x</math> is not there. How many comparisons of <math>x</math> with matrix entries does your algorithm use in worst case? [[5.7|Solution]] ===Divide and Conquer Algorithms=== :5.8 :[[5.9]] :5.10 :[[5.11]] ===Recurrence Relations=== :5.12 :[[5.13]] :5.14 :[[5.15]] :5.16 Back to [[Chapter List]] 95b79ba18b5cf0571c45ccdf0e0c6df00ca7c23c 252 251 2020-09-14T18:40:20Z Algowikiadmin 1 /* Divide and Conquer Algorithms */ wikitext text/x-wiki =Divide and Conquer= ===Binary Search=== :[[5.1]]. Suppose you are given a sorted array <math>A</math> of size n that has been circularly shifted <math>k</math> positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted <math>k = 2</math> positions, while [27, 29, 35, 42, 5, 15] has been shifted <math>k = 4</math> positions. :• Suppose you know what <math>k</math> is. Give an <math>O(1)</math> algorithm to find the largest number in <math>A</math>. :• Suppose you do not know what <math>k</math> is. Give an <math>O(lg n)</math> algorithm to find the largest number in <math>A</math>. For partial credit, you may give an <math>O(n)</math> algorithm. [[5.1|Solution]] :5.2. A sorted array of size n contains distinct integers between <math>1</math> and <math>n + 1</math> , with one element missing. Give an <math>O(log n)</math> algorithm to find the missing integer, without using any extra space. :[[5.3]] Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to <math>n</math>. The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats. :(a) What is an optimal strategy if <math>n</math> in known? :(b) What is a good strategy if <math>n</math> is not known? [[5.3|Solution]] :5.4. You are given a unimodal array of <math>n</math> distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in <math>O(log n)</math> time. :[[5.5]]. Suppose that you are given a sorted sequence of distinct integers <math>[a_1, a_2, . . . , a_n]</math>. Give an <math>O(lg n)</math> algorithm to determine whether there exists an index <math>i</math> such that <math>a_i = i</math>. For example, in [-10, -3, 3, 5, 7], <math>a_3 = 3</math>. In [2, 3, 4, 5, 6, 7], there is no such <math>i</math>. [[5.5|Solution]] :5.6. Suppose that you are given a sorted sequence of distinct integers <math>a = [a_1, a_2, . . . , a_n]</math>, drawn from 1 to <math>m</math> where <math>n < m</math>. Give an <math>O(lg n)</math> algorithm to find an integer <math>\leq m</math> that is not present in <math>a</math>. For full credit, find the smallest such integer <math>x</math> such that <math>1 \leq x \leq m</math>. :[[5.7]]. Let <math>M</math> be an <math>n * m</math> integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer <math>x</math> in <math>M</math>, or to determine that <math>x</math> is not there. How many comparisons of <math>x</math> with matrix entries does your algorithm use in worst case? [[5.7|Solution]] ===Divide and Conquer Algorithms=== :5.8. Given two sorted arrays <math>A</math> and <math>B</math> of size <math>n</math> and <math>m</math> respectively, find the median of the <math>n + m</math> elements. The overall run time complexity should be <math>O(log(m + n))</math>. :[[5.9]]. The largest subrange problem, discussed in Section 5.6, takes an array <math>A</math> of <math>n</math> numbers, and asks for the index pair <math>i</math> and <math>j</math> that maximizes <math>S = \sum_{k=1}^j A[k]</math>. Give an <math>O(n)</math> algorithm for largest subrange. [[5.9|Solution]] :5.10. We are given <math>n</math> wooden sticks, each of integer length, where the <math>i</math>th piece has length <math>L[i]</math>. We seek to cut them so that we end up with <math>k</math> pieces of exactly the same length, in addition to other fragments. Furthermore, we want these <math>k</math> pieces to be as large as possible. :(a) Given four wood sticks, of lengths <math>L = {10, 6, 5, 3}</math>, what are the largest sized pieces you can get for <math>k = 4</math>? (Hint: the answer is not 3). :(b) Give a correct and efficient algorithm that, for a given <math>L</math> and <math>k</math>, returns the maximum possible length of the <math>k</math> equal pieces cut from the initial <math>n</math> sticks. :[[5.11]]. Extend the convolution-based string-matching algorithm described in the text to the case of pattern matching with wildcard characters “*”, which match any character. For example, “sh*t” should match both “shot” and “shut”. [[5.11|Solution]] ===Recurrence Relations=== :5.12 :[[5.13]] :5.14 :[[5.15]] :5.16 Back to [[Chapter List]] 00cec0a8606c0166ae9d184260cd3ef7a63d14bc 253 252 2020-09-14T18:49:23Z Algowikiadmin 1 /* Recurrence Relations */ wikitext text/x-wiki =Divide and Conquer= ===Binary Search=== :[[5.1]]. Suppose you are given a sorted array <math>A</math> of size n that has been circularly shifted <math>k</math> positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted <math>k = 2</math> positions, while [27, 29, 35, 42, 5, 15] has been shifted <math>k = 4</math> positions. :• Suppose you know what <math>k</math> is. Give an <math>O(1)</math> algorithm to find the largest number in <math>A</math>. :• Suppose you do not know what <math>k</math> is. Give an <math>O(lg n)</math> algorithm to find the largest number in <math>A</math>. For partial credit, you may give an <math>O(n)</math> algorithm. [[5.1|Solution]] :5.2. A sorted array of size n contains distinct integers between <math>1</math> and <math>n + 1</math> , with one element missing. Give an <math>O(log n)</math> algorithm to find the missing integer, without using any extra space. :[[5.3]] Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to <math>n</math>. The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats. :(a) What is an optimal strategy if <math>n</math> in known? :(b) What is a good strategy if <math>n</math> is not known? [[5.3|Solution]] :5.4. You are given a unimodal array of <math>n</math> distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in <math>O(log n)</math> time. :[[5.5]]. Suppose that you are given a sorted sequence of distinct integers <math>[a_1, a_2, . . . , a_n]</math>. Give an <math>O(lg n)</math> algorithm to determine whether there exists an index <math>i</math> such that <math>a_i = i</math>. For example, in [-10, -3, 3, 5, 7], <math>a_3 = 3</math>. In [2, 3, 4, 5, 6, 7], there is no such <math>i</math>. [[5.5|Solution]] :5.6. Suppose that you are given a sorted sequence of distinct integers <math>a = [a_1, a_2, . . . , a_n]</math>, drawn from 1 to <math>m</math> where <math>n < m</math>. Give an <math>O(lg n)</math> algorithm to find an integer <math>\leq m</math> that is not present in <math>a</math>. For full credit, find the smallest such integer <math>x</math> such that <math>1 \leq x \leq m</math>. :[[5.7]]. Let <math>M</math> be an <math>n * m</math> integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer <math>x</math> in <math>M</math>, or to determine that <math>x</math> is not there. How many comparisons of <math>x</math> with matrix entries does your algorithm use in worst case? [[5.7|Solution]] ===Divide and Conquer Algorithms=== :5.8. Given two sorted arrays <math>A</math> and <math>B</math> of size <math>n</math> and <math>m</math> respectively, find the median of the <math>n + m</math> elements. The overall run time complexity should be <math>O(log(m + n))</math>. :[[5.9]]. The largest subrange problem, discussed in Section 5.6, takes an array <math>A</math> of <math>n</math> numbers, and asks for the index pair <math>i</math> and <math>j</math> that maximizes <math>S = \sum_{k=1}^j A[k]</math>. Give an <math>O(n)</math> algorithm for largest subrange. [[5.9|Solution]] :5.10. We are given <math>n</math> wooden sticks, each of integer length, where the <math>i</math>th piece has length <math>L[i]</math>. We seek to cut them so that we end up with <math>k</math> pieces of exactly the same length, in addition to other fragments. Furthermore, we want these <math>k</math> pieces to be as large as possible. :(a) Given four wood sticks, of lengths <math>L = {10, 6, 5, 3}</math>, what are the largest sized pieces you can get for <math>k = 4</math>? (Hint: the answer is not 3). :(b) Give a correct and efficient algorithm that, for a given <math>L</math> and <math>k</math>, returns the maximum possible length of the <math>k</math> equal pieces cut from the initial <math>n</math> sticks. :[[5.11]]. Extend the convolution-based string-matching algorithm described in the text to the case of pattern matching with wildcard characters “*”, which match any character. For example, “sh*t” should match both “shot” and “shut”. [[5.11|Solution]] ===Recurrence Relations=== :5.12. In Section 5.3, it is asserted that any polynomial can be represented by a recurrence. Find a recurrence relation that represents the polynomial <math>a_n = n^2</math>. :[[5.13]]. Suppose you are choosing between the following three algorithms: :• Algorithm <math>A</math> solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time. :• Algorithm <math>B</math> solves problems of size <math>n</math> by recursively solving two subproblems of size <math>n - 1</math> and then combining the solutions in constant time. :• Algorithm <math>C</math> solves problems of size <math>n</math> by dividing them into nine subproblems of size <math>n/3</math>, recursively solving each subproblem, and then combining the solutions in <math>\Theta(n^2)</math> time. :What are the running times of each of these algorithms (in big O notation), and which would you choose? [[5.13|Solution]] :5.14. Solve the following recurrence relations and give a <math>\Theta</math> bound for each of them: :(a) <math>T(n) = 2T(n/3) + 1</math> :(b) <math>T(n) = 5T(n/4) + n</math> :(c) <math>T(n) = 7T(n/7) + n</math> :(d) <math>T(n) = 9T(n/3) + n^2</math> :[[5.15]]. Use the master theorem to solve the following recurrence relations: :(a) <math>T(n) = 64T(n/4) + n^4</math> :(b) <math>T(n) = 64T(n/4) + n^3</math> :(c) <math>T(n) = 64T(n/4) + 128</math> [[5.15|Solution]] :5.16. Give asymptotically tight upper (Big Oh) bounds for <math>T(n)</math> in each of the following recurrences. Justify your solutions by naming the particular case of the master theorem, by iterating the recurrence, or by using the substitution method: :(a) <math>T(n) = T(n - 2) + 1</math>. :(b) <math>T(n) = 2T(n/2) + n lg^2 n</math>. :(c) <math>T(n) = 9T(n/4) + n^2</math>. Back to [[Chapter List]] a49064d6456db1c54581ee3dbb9ce9a253fed75d Chapter 3 0 6 254 106 2020-09-14T20:38:22Z Algowikiadmin 1 /* Stacks, Queues, and Lists */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== [[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] 3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) [[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] 3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. [[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] 3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? [[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] d45624b183db57b6e1a378fce6fe95c391f5740a 255 254 2020-09-14T20:39:23Z Algowikiadmin 1 /* Stacks, Queues, and Lists */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== 3.8 [[3.9]] 3.10 ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] da3aa6339027780ad582a529f3a7d98acbf05c80 256 255 2020-09-14T20:42:56Z Algowikiadmin 1 /* Elementary Data Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== 3.34 [[3.35]] 3.36 [[3.37]] 3.38 [[3.39]] 3.40 [[3.41]] 3.42 [[3.43]] 3.44 [[3.45]] Back to [[Chapter List]] 57b371ec0d24b3b0e1b39291aafef5195cf3f7f9 257 256 2020-09-14T20:48:34Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32 [[3.33]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] 236f457bf375df2dbdb2b9765cb16c9f437d7913 258 257 2020-09-18T14:26:48Z Algowikiadmin 1 /* Implementation Projects */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== 3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. [[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] 7a345120834322a292b63f6c92495702189a8be6 259 258 2020-09-18T14:27:02Z Algowikiadmin 1 /* Implementation Projects */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== [[3.11]] 3.12 [[3.13]] 3.14 [[3.15]] 3.16 [[3.17]] 3.18 [[3.19]] 3.20 [[3.21]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] 6375acd3502d8780c103cd4fbd34ea55756c55c9 260 259 2020-09-18T14:43:16Z Algowikiadmin 1 /* Trees and Other Dictionary Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>1, 2, .., n</math>, and initialization can take <math>O(n)</math> time. [[3.11|Solution]] :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an <math>O(n)</math> algorithm to find the maximum depth of a binary tree with <math>n</math> nodes. :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an <math>O(n)</math> algorithm to identify these two elements so they can be swapped back. [[3.13|Solution]] :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order. :[[3.15]]. Describe an <math>O(n)</math>-time algorithm that takes an <math>n</math>-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1. [[3.15|Solution]] :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes: :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes. :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes. :[[3.17]]. Give an <math>O(n)</math> algorithm that determines whether a given <math>n</math>-node binary tree is height-balanced (see Problem 3-15). [[3.17|Solution]] :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this? :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O(log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) [[3.19|Solution]] :3.20. Design a data structure to support the following operations: :• ''insert''(<math>x,T</math>) – Insert item <math>x</math> into the set <math>T</math>. :• ''delete''(<math>k,T</math>) – Delete the <math>k</math>th smallest element from <math>T</math>. :• ''member''(<math>x,T</math>) – Return true iff <math>x \in T</math>. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.21]]. A ''concatenate operation'' takes two sets <math>S_1</math> and <math>S_2</math>, where every key in <math>S_1</math> is smaller than any key in <math>S_2</math>, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal height of the two trees. [[3.21|Solution]] ===Applications of Tree Structures=== 3.22 [[3.23]] 3.24 [[3.25]] 3.26 [[3.27]] 3.28 [[3.29]] 3.30 [[3.31]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] 1b4b8d38c5c002a1df4b19f08b61883f8f1bb5a9 261 260 2020-09-18T15:09:44Z Algowikiadmin 1 /* Applications of Tree Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>1, 2, .., n</math>, and initialization can take <math>O(n)</math> time. [[3.11|Solution]] :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an <math>O(n)</math> algorithm to find the maximum depth of a binary tree with <math>n</math> nodes. :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an <math>O(n)</math> algorithm to identify these two elements so they can be swapped back. [[3.13|Solution]] :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order. :[[3.15]]. Describe an <math>O(n)</math>-time algorithm that takes an <math>n</math>-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1. [[3.15|Solution]] :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes: :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes. :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes. :[[3.17]]. Give an <math>O(n)</math> algorithm that determines whether a given <math>n</math>-node binary tree is height-balanced (see Problem 3-15). [[3.17|Solution]] :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this? :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O(log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) [[3.19|Solution]] :3.20. Design a data structure to support the following operations: :• ''insert''(<math>x,T</math>) – Insert item <math>x</math> into the set <math>T</math>. :• ''delete''(<math>k,T</math>) – Delete the <math>k</math>th smallest element from <math>T</math>. :• ''member''(<math>x,T</math>) – Return true iff <math>x \in T</math>. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.21]]. A ''concatenate operation'' takes two sets <math>S_1</math> and <math>S_2</math>, where every key in <math>S_1</math> is smaller than any key in <math>S_2</math>, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal height of the two trees. [[3.21|Solution]] ===Applications of Tree Structures=== :3.22. Design a data structure that supports the following two operations: :• ''insert''(<math>x</math>) – Insert item <math>x</math> from the data stream to the data structure. :• ''median''() – Return the median of all elements so far. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of <math>n</math> strings, each of length at most <math>l</math>. We seek to print out all strings beginning with a particular prefix <math>p</math>. Show how to do this in <math>O(ml log n)</math> time, where <math>m</math> is the number of strings. [[3.23|Solution]] :3.24. An array <math>A</math> is called <math>k</math>-unique if it does not contain a pair of duplicate elements within <math>k</math> positions of each other, that is, there is no <math>i</math> and <math>j</math> such that <math>A[i] = A[j]</math> and <math>|j - i| \leq k</math>. Design a worst-case <math>O(n log k)</math> algorithm to test if <math>A</math> is <math>k</math>-unique. :[[3.25]]. In the ''bin-packing problem'', we are given <math>n</math> objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, with each bin holding 1 kilogram at most. :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the <math>n</math> weights <math>w_1, w_2, ..., w_n</math> and outputting the number of bins used) in <math>O(n log n)</math> time. :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted. [[3.25|Solution]] :3.26. Suppose that we are given a sequence of <math>n</math> values <math>x_1, x_2, ..., x_n</math> and seek to quickly answer repeated queries of the form: given <math>i</math> and <math>j</math>, find the smallest value in <math>x_i, . . . , x_j</math>. :(a) Design a data structure that uses <math>O(n^2)</math> space and answers queries in <math>O(1)</math> time. :(b) Design a data structure that uses <math>O(n)</math> space and answers queries in <math>O(log n)</math> time. For partial credit, your data structure can use <math>O(n log n)</math> space and have <math>O(log n)</math> query time. :[[3.27]]. Suppose you are given an input set <math>S</math> of <math>n</math> integers, and a black box that if given any sequence of integers and an integer <math>k</math> instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly <math>k</math>. Show how to use the black box <math>O(n)</math> times to find a subset of <math>S</math> that adds up to <math>k</math>. [[3.27|Solution]] :3.28. Let <math>A[1..n]</math> be an array of real numbers. Design an algorithm to perform any sequence of the following operations: :• ''Add''(<math>i,y</math>) – Add the value <math>y</math> to the <math>i</math>th number. :• ''Partial-sum''(<math>i</math>) – Return the sum of the first <math>i</math> numbers, that is, <math>\sum_{j=1}^i A[j]</math>. :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take <math>O(log n)</math> steps. You may use one additional array of size <math>n</math> as a work space. :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different. :• 'Add''(<math>k,y</math>) – Add the value <math>y</math> to the item with key <math>k</math>. :• ''Insert''(<math>k,y</math>) – Insert a new item with key <math>k</math> and value <math>y</math>. :• ''Delete''(<math>k</math>) – Delete the item with key <math>k</math>. :• ''Partial-sum''(<math>k</math>) – Return the sum of all the elements currently in the set whose key is less than <math>k</math>, that is, <math>\sum_{i < k} x_i</math>. :The worst-case running time should still be <math>O(n log n)</math> for any sequence of <math>O(n)</math> operations. [[3.29|Solution]] :3.30. You are consulting for a hotel that has <math>n</math> one-bed rooms. When a guest checks in, they ask for a room whose number is in the range <math>[l, h]</math>. Propose a data structure that supports the following data operations in the allotted time: :(a) ''Initialize''(<math>n</math>): Initialize the data structure for empty rooms numbered <math>1, 2, . . . , n</math>, in polynomial time. :(b) ''Count''(<math>l, h</math>): Return the number of available rooms in <math>[l, h]</math>, in <math>O(log n)</math> time. :(c) ''Checkin''(<math>l, h</math>): In <math>O(log n)</math> time, return the first empty room in <math>[l, h]</math> and mark it occupied, or return NIL if all the rooms in <math>[l, h]</math> are occupied. :(d) ''Checkout''(<math>x</math>): Mark room <math>x</math> as unoccupied, in <math>O(log n)</math> time. :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer <math>X</math> in <math>O(1)</math> time (i.e., constant time, independent of the total number of integers stored). Assume that <math>1 \leq X \leq n</math> and that there are <math>m + n</math> units of space available, where <math>m</math> is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays <math>A[1..n]</math> and <math>B[1..m]</math>.) You are not allowed to initialize either <math>A</math> or <math>B</math>, as that would take <math>O(m)</math> or <math>O(n)</math> operations. This means the arrays are full of random garbage to begin with, so you must be very careful. [[3.31|Solution]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] f788c06c68ad2fc3609888d7a1d7171361886692 262 261 2020-09-18T15:10:36Z Algowikiadmin 1 /* Applications of Tree Structures */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>1, 2, .., n</math>, and initialization can take <math>O(n)</math> time. [[3.11|Solution]] :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an <math>O(n)</math> algorithm to find the maximum depth of a binary tree with <math>n</math> nodes. :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an <math>O(n)</math> algorithm to identify these two elements so they can be swapped back. [[3.13|Solution]] :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order. :[[3.15]]. Describe an <math>O(n)</math>-time algorithm that takes an <math>n</math>-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1. [[3.15|Solution]] :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes: :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes. :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes. :[[3.17]]. Give an <math>O(n)</math> algorithm that determines whether a given <math>n</math>-node binary tree is height-balanced (see Problem 3-15). [[3.17|Solution]] :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this? :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O(log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) [[3.19|Solution]] :3.20. Design a data structure to support the following operations: :• ''insert''(<math>x,T</math>) – Insert item <math>x</math> into the set <math>T</math>. :• ''delete''(<math>k,T</math>) – Delete the <math>k</math>th smallest element from <math>T</math>. :• ''member''(<math>x,T</math>) – Return true iff <math>x \in T</math>. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.21]]. A ''concatenate operation'' takes two sets <math>S_1</math> and <math>S_2</math>, where every key in <math>S_1</math> is smaller than any key in <math>S_2</math>, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal height of the two trees. [[3.21|Solution]] ===Applications of Tree Structures=== :3.22. Design a data structure that supports the following two operations: :• ''insert''(<math>x</math>) – Insert item <math>x</math> from the data stream to the data structure. :• ''median''() – Return the median of all elements so far. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of <math>n</math> strings, each of length at most <math>l</math>. We seek to print out all strings beginning with a particular prefix <math>p</math>. Show how to do this in <math>O(ml log n)</math> time, where <math>m</math> is the number of strings. [[3.23|Solution]] :3.24. An array <math>A</math> is called <math>k</math>-unique if it does not contain a pair of duplicate elements within <math>k</math> positions of each other, that is, there is no <math>i</math> and <math>j</math> such that <math>A[i] = A[j]</math> and <math>|j - i| \leq k</math>. Design a worst-case <math>O(n log k)</math> algorithm to test if <math>A</math> is <math>k</math>-unique. :[[3.25]]. In the ''bin-packing problem'', we are given <math>n</math> objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, with each bin holding 1 kilogram at most. :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the <math>n</math> weights <math>w_1, w_2, ..., w_n</math> and outputting the number of bins used) in <math>O(n log n)</math> time. :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted. [[3.25|Solution]] :3.26. Suppose that we are given a sequence of <math>n</math> values <math>x_1, x_2, ..., x_n</math> and seek to quickly answer repeated queries of the form: given <math>i</math> and <math>j</math>, find the smallest value in <math>x_i, . . . , x_j</math>. :(a) Design a data structure that uses <math>O(n^2)</math> space and answers queries in <math>O(1)</math> time. :(b) Design a data structure that uses <math>O(n)</math> space and answers queries in <math>O(log n)</math> time. For partial credit, your data structure can use <math>O(n log n)</math> space and have <math>O(log n)</math> query time. :[[3.27]]. Suppose you are given an input set <math>S</math> of <math>n</math> integers, and a black box that if given any sequence of integers and an integer <math>k</math> instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly <math>k</math>. Show how to use the black box <math>O(n)</math> times to find a subset of <math>S</math> that adds up to <math>k</math>. [[3.27|Solution]] :3.28. Let <math>A[1..n]</math> be an array of real numbers. Design an algorithm to perform any sequence of the following operations: :• ''Add''(<math>i,y</math>) – Add the value <math>y</math> to the <math>i</math>th number. :• ''Partial-sum''(<math>i</math>) – Return the sum of the first <math>i</math> numbers, that is, <math>\sum_{j=1}^i A[j]</math>. :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take <math>O(log n)</math> steps. You may use one additional array of size <math>n</math> as a work space. :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different. :• ''Add''(<math>k,y</math>) – Add the value <math>y</math> to the item with key <math>k</math>. :• ''Insert''(<math>k,y</math>) – Insert a new item with key <math>k</math> and value <math>y</math>. :• ''Delete''(<math>k</math>) – Delete the item with key <math>k</math>. :• ''Partial-sum''(<math>k</math>) – Return the sum of all the elements currently in the set whose key is less than <math>k</math>, that is, <math>\sum_{i < k} x_i</math>. :The worst-case running time should still be <math>O(n log n)</math> for any sequence of <math>O(n)</math> operations. [[3.29|Solution]] :3.30. You are consulting for a hotel that has <math>n</math> one-bed rooms. When a guest checks in, they ask for a room whose number is in the range <math>[l, h]</math>. Propose a data structure that supports the following data operations in the allotted time: :(a) ''Initialize''(<math>n</math>): Initialize the data structure for empty rooms numbered <math>1, 2, . . . , n</math>, in polynomial time. :(b) ''Count''(<math>l, h</math>): Return the number of available rooms in <math>[l, h]</math>, in <math>O(log n)</math> time. :(c) ''Checkin''(<math>l, h</math>): In <math>O(log n)</math> time, return the first empty room in <math>[l, h]</math> and mark it occupied, or return NIL if all the rooms in <math>[l, h]</math> are occupied. :(d) ''Checkout''(<math>x</math>): Mark room <math>x</math> as unoccupied, in <math>O(log n)</math> time. :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer <math>X</math> in <math>O(1)</math> time (i.e., constant time, independent of the total number of integers stored). Assume that <math>1 \leq X \leq n</math> and that there are <math>m + n</math> units of space available, where <math>m</math> is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays <math>A[1..n]</math> and <math>B[1..m]</math>.) You are not allowed to initialize either <math>A</math> or <math>B</math>, as that would take <math>O(m)</math> or <math>O(n)</math> operations. This means the arrays are full of random garbage to begin with, so you must be very careful. [[3.31|Solution]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. [4] Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] 6ac541812166fee1d691bb7dfe6b6d018d9af244 297 262 2020-09-20T18:11:43Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Data Structure= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>1, 2, .., n</math>, and initialization can take <math>O(n)</math> time. [[3.11|Solution]] :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an <math>O(n)</math> algorithm to find the maximum depth of a binary tree with <math>n</math> nodes. :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an <math>O(n)</math> algorithm to identify these two elements so they can be swapped back. [[3.13|Solution]] :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order. :[[3.15]]. Describe an <math>O(n)</math>-time algorithm that takes an <math>n</math>-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1. [[3.15|Solution]] :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes: :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes. :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes. :[[3.17]]. Give an <math>O(n)</math> algorithm that determines whether a given <math>n</math>-node binary tree is height-balanced (see Problem 3-15). [[3.17|Solution]] :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this? :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O(log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) [[3.19|Solution]] :3.20. Design a data structure to support the following operations: :• ''insert''(<math>x,T</math>) – Insert item <math>x</math> into the set <math>T</math>. :• ''delete''(<math>k,T</math>) – Delete the <math>k</math>th smallest element from <math>T</math>. :• ''member''(<math>x,T</math>) – Return true iff <math>x \in T</math>. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.21]]. A ''concatenate operation'' takes two sets <math>S_1</math> and <math>S_2</math>, where every key in <math>S_1</math> is smaller than any key in <math>S_2</math>, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal height of the two trees. [[3.21|Solution]] ===Applications of Tree Structures=== :3.22. Design a data structure that supports the following two operations: :• ''insert''(<math>x</math>) – Insert item <math>x</math> from the data stream to the data structure. :• ''median''() – Return the median of all elements so far. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of <math>n</math> strings, each of length at most <math>l</math>. We seek to print out all strings beginning with a particular prefix <math>p</math>. Show how to do this in <math>O(ml log n)</math> time, where <math>m</math> is the number of strings. [[3.23|Solution]] :3.24. An array <math>A</math> is called <math>k</math>-unique if it does not contain a pair of duplicate elements within <math>k</math> positions of each other, that is, there is no <math>i</math> and <math>j</math> such that <math>A[i] = A[j]</math> and <math>|j - i| \leq k</math>. Design a worst-case <math>O(n log k)</math> algorithm to test if <math>A</math> is <math>k</math>-unique. :[[3.25]]. In the ''bin-packing problem'', we are given <math>n</math> objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, with each bin holding 1 kilogram at most. :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the <math>n</math> weights <math>w_1, w_2, ..., w_n</math> and outputting the number of bins used) in <math>O(n log n)</math> time. :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted. [[3.25|Solution]] :3.26. Suppose that we are given a sequence of <math>n</math> values <math>x_1, x_2, ..., x_n</math> and seek to quickly answer repeated queries of the form: given <math>i</math> and <math>j</math>, find the smallest value in <math>x_i, . . . , x_j</math>. :(a) Design a data structure that uses <math>O(n^2)</math> space and answers queries in <math>O(1)</math> time. :(b) Design a data structure that uses <math>O(n)</math> space and answers queries in <math>O(log n)</math> time. For partial credit, your data structure can use <math>O(n log n)</math> space and have <math>O(log n)</math> query time. :[[3.27]]. Suppose you are given an input set <math>S</math> of <math>n</math> integers, and a black box that if given any sequence of integers and an integer <math>k</math> instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly <math>k</math>. Show how to use the black box <math>O(n)</math> times to find a subset of <math>S</math> that adds up to <math>k</math>. [[3.27|Solution]] :3.28. Let <math>A[1..n]</math> be an array of real numbers. Design an algorithm to perform any sequence of the following operations: :• ''Add''(<math>i,y</math>) – Add the value <math>y</math> to the <math>i</math>th number. :• ''Partial-sum''(<math>i</math>) – Return the sum of the first <math>i</math> numbers, that is, <math>\sum_{j=1}^i A[j]</math>. :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take <math>O(log n)</math> steps. You may use one additional array of size <math>n</math> as a work space. :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different. :• ''Add''(<math>k,y</math>) – Add the value <math>y</math> to the item with key <math>k</math>. :• ''Insert''(<math>k,y</math>) – Insert a new item with key <math>k</math> and value <math>y</math>. :• ''Delete''(<math>k</math>) – Delete the item with key <math>k</math>. :• ''Partial-sum''(<math>k</math>) – Return the sum of all the elements currently in the set whose key is less than <math>k</math>, that is, <math>\sum_{i < k} x_i</math>. :The worst-case running time should still be <math>O(n log n)</math> for any sequence of <math>O(n)</math> operations. [[3.29|Solution]] :3.30. You are consulting for a hotel that has <math>n</math> one-bed rooms. When a guest checks in, they ask for a room whose number is in the range <math>[l, h]</math>. Propose a data structure that supports the following data operations in the allotted time: :(a) ''Initialize''(<math>n</math>): Initialize the data structure for empty rooms numbered <math>1, 2, . . . , n</math>, in polynomial time. :(b) ''Count''(<math>l, h</math>): Return the number of available rooms in <math>[l, h]</math>, in <math>O(log n)</math> time. :(c) ''Checkin''(<math>l, h</math>): In <math>O(log n)</math> time, return the first empty room in <math>[l, h]</math> and mark it occupied, or return NIL if all the rooms in <math>[l, h]</math> are occupied. :(d) ''Checkout''(<math>x</math>): Mark room <math>x</math> as unoccupied, in <math>O(log n)</math> time. :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer <math>X</math> in <math>O(1)</math> time (i.e., constant time, independent of the total number of integers stored). Assume that <math>1 \leq X \leq n</math> and that there are <math>m + n</math> units of space available, where <math>m</math> is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays <math>A[1..n]</math> and <math>B[1..m]</math>.) You are not allowed to initialize either <math>A</math> or <math>B</math>, as that would take <math>O(m)</math> or <math>O(n)</math> operations. This means the arrays are full of random garbage to begin with, so you must be very careful. [[3.31|Solution]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] f4b0354629ed2dc61498b07f795c5fb4d0383c85 Chapter 4 0 7 263 111 2020-09-18T15:28:59Z Algowikiadmin 1 /* Applications of Sorting: Numbers */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]] :4.6 :[[4.7]] :4.8 :[[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] dbed97b44c5c61af890ac2f253b49a5a7c1879fd 264 263 2020-09-18T15:29:25Z Algowikiadmin 1 /* Sorting */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]] :4.6 :[[4.7]] :4.8 :[[4.9]] :4.10 :[[4.11]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 9820ea3eb0289d14c75a1c8f09f01d1cfe1ab34e 266 264 2020-09-19T18:52:01Z Algowikiadmin 1 /* Applications of Sorting: Numbers */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 204453359ed8d298010fed78d9d09fdcafd239e9 267 266 2020-09-19T18:52:36Z Algowikiadmin 1 /* Applications of Sorting: Numbers */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applicatoins of Sorting: Intervals and Sets=== :4.12 :[[4.13]] :4.14 :[[4.15]] :4.16 ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] f8b1ae53fbba5dbcc5fd021f5b34eac006379e84 268 267 2020-09-19T19:35:26Z Algowikiadmin 1 /* Applicatoins of Sorting: Intervals and Sets */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be {(1, 6),(7, 18)}. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 953351a6ed9d5d0903007120e82aca0740a28852 269 268 2020-09-19T19:36:03Z Algowikiadmin 1 /* Applications of Sorting: Intervals and Sets */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]] :4.18 :[[4.19]] :4.20 ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] fcd98bc8d2a3290754fb6d202c5beca6133afac8 270 269 2020-09-19T19:39:59Z Algowikiadmin 1 /* Heaps */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]] :4.22 :[[4.23]] :4.24 :[[4.25]] :4.26 :[[4.27]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 4d98b0b93fb0d64ac41e4dd4ac266260f23ceed8 271 270 2020-09-19T19:52:58Z Algowikiadmin 1 /* Quicksort */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28 :[[4.29]] :4.30 ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 7e8c775b0cfeac14bce98de381e4e2310dafc767 272 271 2020-09-19T19:58:10Z Algowikiadmin 1 /* Mergesort */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]] :4.32 :[[4.33]] :4.34 :[[4.35]] :4.36 :[[4.37]] :4.38 ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 848f360a987e64cebfe56e9a9f48d9d21fb75a99 273 272 2020-09-19T20:34:16Z Algowikiadmin 1 /* Other Sorting Alogrithims */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. [[4.31|Solution]] :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. [[4.33|Solution]] :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. [[4.35|Solution]] :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. [[4.37|Solution]] :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. ===Lower Bounds=== :[[4.39]] :4.40 ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 2fd2dd7f2b71d24bafce1baa5b481551164f60de 274 273 2020-09-19T20:37:18Z Algowikiadmin 1 /* Lower Bounds */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. [[4.31|Solution]] :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. [[4.33|Solution]] :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. [[4.35|Solution]] :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. [[4.37|Solution]] :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. ===Lower Bounds=== :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in <math>O(n log(\sqrt n))</math>. Given the existence of an <math>\Omega(n log n)</math> lower bound for sorting, how can this be possible? [[4.39|Solution]] :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in <math>O(1)</math> worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the <math>\Omega (n log n)</math> lower bound for sorting.) ===Searching=== :[[4.41]] :4.42 ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 5515b864d9872d7479711c23a2eb12ba55cd3f1a 275 274 2020-09-19T20:42:06Z Algowikiadmin 1 /* Searching */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. [[4.31|Solution]] :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. [[4.33|Solution]] :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. [[4.35|Solution]] :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. [[4.37|Solution]] :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. ===Lower Bounds=== :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in <math>O(n log(\sqrt n))</math>. Given the existence of an <math>\Omega(n log n)</math> lower bound for sorting, how can this be possible? [[4.39|Solution]] :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in <math>O(1)</math> worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the <math>\Omega (n log n)</math> lower bound for sorting.) ===Searching=== :[[4.41]]. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database: :• Put all the names in a single array and use binary search. :• Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array. :Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options? [[4.41|Solution]] :4.42. A ''Ramanujan number'' can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers <math>a, b, c</math>, and <math>d</math> such that <math>a^3 + b^3 = c^3 + d^3</math>. For example, 1729 is a Ramanujan number because <math>1729 = 1^3 + 12^3 = 9^3 + 10^3</math>. :(a) Give an efficient algorithm to test whether a given single integer <math>n</math> is a Ramanujan number, with an analysis of the algorithm’s complexity. :(b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and <math>n</math>, with an analysis of its complexity. ===Implementaion Challenges=== :[[4.43]] :4.44 :[[4.45]] :4.46 ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] f70cece19a3e6b951cc1ce5b9ee433c24e4ab4e4 276 275 2020-09-19T20:44:41Z Algowikiadmin 1 /* Implementaion Challenges */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. [[4.31|Solution]] :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. [[4.33|Solution]] :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. [[4.35|Solution]] :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. [[4.37|Solution]] :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. ===Lower Bounds=== :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in <math>O(n log(\sqrt n))</math>. Given the existence of an <math>\Omega(n log n)</math> lower bound for sorting, how can this be possible? [[4.39|Solution]] :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in <math>O(1)</math> worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the <math>\Omega (n log n)</math> lower bound for sorting.) ===Searching=== :[[4.41]]. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database: :• Put all the names in a single array and use binary search. :• Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array. :Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options? [[4.41|Solution]] :4.42. A ''Ramanujan number'' can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers <math>a, b, c</math>, and <math>d</math> such that <math>a^3 + b^3 = c^3 + d^3</math>. For example, 1729 is a Ramanujan number because <math>1729 = 1^3 + 12^3 = 9^3 + 10^3</math>. :(a) Give an efficient algorithm to test whether a given single integer <math>n</math> is a Ramanujan number, with an analysis of the algorithm’s complexity. :(b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and <math>n</math>, with an analysis of its complexity. ===Implementaion Challenges=== :[[4.43]]. Consider an n×n array <math>A</math> containing integer elements (positive, negative, and zero). Assume that the elements in each row of <math>A</math> are in strictly increasing order, and the elements of each column of <math>A</math> are in strictly decreasing order. (Hence there cannot be two zeros in the same row or the same column.) Describe an efficient algorithm that counts the number of occurrences of the element 0 in <math>A</math>. Analyze its running time. [[4.43|Solution]] :4.44. Implement versions of several different sorting algorithms, such as selection sort, insertion sort, heapsort, mergesort, and quicksort. Conduct experiments to assess the relative performance of these algorithms in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by sorting all the words that occur in the text and then passing through the sorted sequence to identify one instance of each distinct word. Write a brief report with your conclusions. :[[4.45]]. Implement an external sort, which uses intermediate files to sort files bigger than main memory. Mergesort is a good algorithm to base such an implementation on. Test your program both on files with small records and on files with large records. [[4.45|Solution]] :4.46. Design and implement a parallel sorting algorithm that distributes data across several processors. An appropriate variation of mergesort is a likely candidate. Measure the speedup of this algorithm as the number of processors increases. Then compare the execution time to that of a purely sequential mergesort implementation. What are your experiences? ===Interview Problems=== :[[4.47]] :4.48 :[[4.49]] :4.50 :[[4.51]] :4.52 :[[4.53]] Back to [[Chapter List]] 796824c2d558183357d1244f8dbaedd3995ec696 277 276 2020-09-19T20:48:10Z Algowikiadmin 1 /* Interview Problems */ wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. [[4.31|Solution]] :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. [[4.33|Solution]] :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. [[4.35|Solution]] :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. [[4.37|Solution]] :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. ===Lower Bounds=== :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in <math>O(n log(\sqrt n))</math>. Given the existence of an <math>\Omega(n log n)</math> lower bound for sorting, how can this be possible? [[4.39|Solution]] :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in <math>O(1)</math> worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the <math>\Omega (n log n)</math> lower bound for sorting.) ===Searching=== :[[4.41]]. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database: :• Put all the names in a single array and use binary search. :• Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array. :Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options? [[4.41|Solution]] :4.42. A ''Ramanujan number'' can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers <math>a, b, c</math>, and <math>d</math> such that <math>a^3 + b^3 = c^3 + d^3</math>. For example, 1729 is a Ramanujan number because <math>1729 = 1^3 + 12^3 = 9^3 + 10^3</math>. :(a) Give an efficient algorithm to test whether a given single integer <math>n</math> is a Ramanujan number, with an analysis of the algorithm’s complexity. :(b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and <math>n</math>, with an analysis of its complexity. ===Implementaion Challenges=== :[[4.43]]. Consider an n×n array <math>A</math> containing integer elements (positive, negative, and zero). Assume that the elements in each row of <math>A</math> are in strictly increasing order, and the elements of each column of <math>A</math> are in strictly decreasing order. (Hence there cannot be two zeros in the same row or the same column.) Describe an efficient algorithm that counts the number of occurrences of the element 0 in <math>A</math>. Analyze its running time. [[4.43|Solution]] :4.44. Implement versions of several different sorting algorithms, such as selection sort, insertion sort, heapsort, mergesort, and quicksort. Conduct experiments to assess the relative performance of these algorithms in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by sorting all the words that occur in the text and then passing through the sorted sequence to identify one instance of each distinct word. Write a brief report with your conclusions. :[[4.45]]. Implement an external sort, which uses intermediate files to sort files bigger than main memory. Mergesort is a good algorithm to base such an implementation on. Test your program both on files with small records and on files with large records. [[4.45|Solution]] :4.46. Design and implement a parallel sorting algorithm that distributes data across several processors. An appropriate variation of mergesort is a likely candidate. Measure the speedup of this algorithm as the number of processors increases. Then compare the execution time to that of a purely sequential mergesort implementation. What are your experiences? ===Interview Problems=== :[[4.47]]. If you are given a million integers to sort, what algorithm would you use to sort them? How much time and memory would that consume? [[4.47|Solution]] :4.48. Describe advantages and disadvantages of the most popular sorting algorithms. :[[4.49]]. Implement an algorithm that takes an input array and returns only the unique elements in it. [[4.49|Solution]] :4.50. You have a computer with only 4 GB of main memory. How do you use it to sort a large file of 500 GB that is on disk? :[[4.51]]. Design a stack that supports push, pop, and retrieving the minimum element in constant time. [[4.51|Solution]] :4.52. Given a search string of three words, find the smallest snippet of the document that contains all three of the search words—that is, the snippet with the smallest number of words in it. You are given the index positions where these words occur in the document, such as ''word1'': <math>(1, 4, 5)</math>, ''word2'': <math>(3, 9, 10)</math>, and ''word3'': <math>(2, 6, 15)</math>. Each of the lists are in sorted order, as above. :[[4.53]]. You are given twelve coins. One of them is heavier or lighter than the rest. Identify this coin in just three weighings with a balance scale. [[4.53|Solution]] Back to [[Chapter List]] e58fc8593883cda05ec646236632e3a442dbc043 Chapter 2 0 4 265 161 2020-09-19T18:20:30Z Algowikiadmin 1 /* Big Oh */ wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>\Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2^{klog n}, g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^{2n}</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n^{\pi} & \pi^n & \binom{n}{5} & \sqrt{2\sqrt{n}} \\ \binom{n}{n-4} & 2^{log^4n} & n^{5(logn)^2} & n^4\binom{n}{n-4} \\ \end{array}</math> </center> :[[2.25]]. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} \sum_{i=1}^n i^i & n^n & (log n)^{log n} & 2^{(log n^2)}\\ n! & 2^{log^4n} & n^{(log n)^2} & n^4 \binom{n}{n-4}\\ \end{array}</math> </center> [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 2f3ff358480c862a95b26df16fb714efe1700457 3.1 0 92 278 2020-09-20T17:56:57Z Algowikiadmin 1 Created page with "You just need to maintain a count, like so: <pre> public static boolean isBalanced(String str) { int count = 0; for (int i = 0, n = str.length(); i < n;..." wikitext text/x-wiki You just need to maintain a count, like so: <pre> public static boolean isBalanced(String str) { int count = 0; for (int i = 0, n = str.length(); i < n; i++) { switch (str.charAt(i)) { case '(': count++; break; case ')': count--; break; default: throw new IllegalArgumentException(); } if (count < 0) { System.out.println("Imbalance at index " + i); return false; } } if (count != 0) { System.out.println("Imbalance at index " + (str.length() - 1)); return false; } return true; } </pre> Another Approach to use stack in-order to solve this problem; A. Read the string from left to right as CHR B. Keep pushing CHR until CHR is not ")" 1) Pop from stack once CHR == ")" if poped element is NOT "(" return FAlse Repeat A --[[User:Max|Max]] 07:18, 16 June 2010 (EDT) ---- The approach below maintains a stack where the index of each opening '(' is pushed onto the stack. As each closing ')' is found, the index of the matching '(' is popped off the stack. The string is unbalanced if there: * is an attempt to pop an element off an empty stack. The position of the current character can be reported as the position of the first offending parenthesis. * are any elements left on the stack. The value of the first element in the stack can be reported as the position of the first offending parenthesis. <pre> #include <iostream> #include <stack> #include <string> int main (int argc, char* argv[]) { if (argc != 2) { std::cout << "pass a string to check if it's parens are balanced" << std::endl; exit(1); } std::stack<int> parens; std::string input = argv[1]; for (int i = 0; i < input.length(); i++) { char ch = input[i]; if (ch == ')') { if (parens.size() == 0) { std::cout << "unmatched ')' at position: " << i << std::endl; exit(1); } parens.pop(); } else if (ch == '(') { parens.push(i); } } if (parens.size() != 0) { while (parens.size() > 1) { parens.pop(); } std::cout << "unmatched '(' at position: " << parens.top() << std::endl; exit(1); } else { std::cout << "parens balanced!" << std::endl; } } </pre> --[[User:Neonstalwart|Neonstalwart]] ([[User talk:Neonstalwart|talk]]) 22:47, 26 March 2015 (EDT) ---- Max's first example above appears to incorrectly report the last character in the string as the offending index if there is a surplus of opening parentheses. The following solution (in JavaScript) takes a similar approach, but correctly reports the index of the first unmatched open parenthesis (more like Neonstalwart's solution, but without requiring a stack). This function returns a number: -1 if balanced, otherwise the index of the first offending parenthesis. <pre> function checkParens(str) { var numOpen = 0; var length = str.length; var offender; for (var i = 0; i < length; i++) { if (str[i] === '(') { if (!numOpen) { // Store index of first open paren in case it is never closed offender = i; } numOpen++; } else if (str[i] === ')') { if (!numOpen) { // Closing paren with none open = instant fail return i; } numOpen--; } } return numOpen === 0 ? -1 : offender; } </pre> --[[User:CnEY|CnEY]] ([[User talk:CnEY|talk]]) 23:15, 27 August 2015 (EDT) Back to [[Chapter 3]] 8a6c8c3dd3795c2218006bc524c774ae37508437 3.3 0 93 279 2020-09-20T17:57:59Z Algowikiadmin 1 Created page with "'''C''' <pre> typedef struct Node { char *value; struct Node *next; } Node; int reverse(Node **head) { Node *curr, *prev, *next; if (!head || !(*head)) {..." wikitext text/x-wiki '''C''' <pre> typedef struct Node { char *value; struct Node *next; } Node; int reverse(Node **head) { Node *curr, *prev, *next; if (!head || !(*head)) { return 0; } curr = *head; prev = NULL; next = NULL; while (curr) { next = curr->next; curr->next = prev; prev = curr; curr = next; } *head = prev; return 1; } </pre> ---- '''Dart''' <pre> class Node<T> { Node(this.value, [this.next]); T value; Node next; @override String toString() => '$value $next'; } Node reverse(Node n) { Node curr = n.next; n.next = null; // `n` is the new tail. while(curr != null) { Node tmp = curr.next; // Start swap. curr.next = n; // next => previous n = curr; // Update loop's previous node. curr = tmp; // Update loop's current node. } return n; } // Example void main() { final list = Node(1, Node(2, Node(3, Node(4, Node(5))))); print(list); // 1 2 3 4 5 null print(reverse(list)); // 5 4 3 2 1 null } </pre> ---- '''In Java ''' <pre> class ListNode { int val; ListNode next; ListNode(final int x) { val = x; next = null; } } public class LinkedList { ListNode head; public LinkedList() { head = null; } public void prepend(final int in) { final ListNode data = new ListNode(in); data.next = head; head = data; } public void appendToList(final int in) { final ListNode data = new ListNode(in); ListNode current = head; if (head == null) { head = data; return; } while (current.next != null) { current = current.next; } current.next = data; } public void printList(final ListNode head) { ListNode current = head; while (current != null) { System.out.print(current.val + " -> "); current = current.next; } System.out.println(); } public ListNode reverseList(final ListNode node) { //what if 0 node in list if (node == null) { return null; } //what if 1 node in list if (node.next == null) { return node; } //what if multiple nodes.divide into two parts . reverse second part recursively and in end point second.next to first final ListNode secondElem = node.next; node.next = null; final ListNode reverseRest = reverseList(secondElem); secondElem.next = node; return reverseRest; } public static void main(final String[] args) { final LinkedList linkedList = new LinkedList(); linkedList.appendToList(12); linkedList.appendToList(13); linkedList.appendToList(14); linkedList.appendToList(15); linkedList.printList(linkedList.head); final ListNode head = linkedList.reverseList(linkedList.head); linkedList.printList(head); } } </pre> ---- '''In Python ''' <pre> class Node(): def __init__(self, value): self.value = value self.next = None class LinkedList(): def __init__(self): self.head = None self.tail = None def add_node(self, value): if not self.head: self.__initialize_list(value) else: node = Node(value) self.tail.next = node self.tail = node def print_list(self): current_node = self.head print('head ->'), while current_node is not None: print(current_node.value), current_node = current_node.next print('->'), print('tail') def reverse(self): current_head = self.head head = self.head while current_head is not None: temp = current_head current_head = head.next t_next = current_head.next head.next = t_next current_head.next = temp if head.next is None: self.head = current_head break def __initialize_list(self, value): self.head = Node(value) self.tail = self.head </pre> ---- Back to [[Chapter 3]] e557e8b09e0d59c97856f680bb0701faabb041df 3.5 0 94 280 2020-09-20T17:59:03Z Algowikiadmin 1 Created page with "1. size 4 array with 3 elements. remove 1, insert 1, and so forth. <br> 2. when the array is one-fourth full, shrink its size to half of what it was. Back to [[Chaprer 3]]" wikitext text/x-wiki 1. size 4 array with 3 elements. remove 1, insert 1, and so forth. <br> 2. when the array is one-fourth full, shrink its size to half of what it was. Back to [[Chaprer 3]] ea8d7c401c21abb490b9393e0eb80e48202cd93b 281 280 2020-09-20T17:59:16Z Algowikiadmin 1 wikitext text/x-wiki 1. size 4 array with 3 elements. remove 1, insert 1, and so forth. <br> 2. when the array is one-fourth full, shrink its size to half of what it was. Back to [[Chapter 3]] afc8d8ce8dfb830d900c00595238306ca6d6f814 3.7 0 95 282 2020-09-20T17:59:47Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.9 0 96 283 2020-09-20T18:00:56Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.11 0 97 284 2020-09-20T18:02:00Z Algowikiadmin 1 Created page with "Since 1,2,...,n is finite, use a bit array to represent them.<br> See the telephone number sorting example in Column 1 of <Programming Pearls> (Jon Bentley) for detailed expla..." wikitext text/x-wiki Since 1,2,...,n is finite, use a bit array to represent them.<br> See the telephone number sorting example in Column 1 of <Programming Pearls> (Jon Bentley) for detailed explanation. Back to [[Chapter 3]] 366f0b40823b54b0f84887a990ae105f8eb67462 3.13 0 98 285 2020-09-20T18:02:41Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.15 0 99 286 2020-09-20T18:03:24Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.17 0 100 287 2020-09-20T18:03:41Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.19 0 101 288 2020-09-20T18:04:27Z Algowikiadmin 1 Created page with "Store two values, the maximum and minimum. These need to be checked and potentially updated on every delete. If the minimum is being deleted, call the successor, update the..." wikitext text/x-wiki Store two values, the maximum and minimum. These need to be checked and potentially updated on every delete. If the minimum is being deleted, call the successor, update the minimum and delete the old item. Similarly, if the maximum is being deleted, call the predecessor, update the maximum and delete the old item. On insert, compare the new item to min/max, if it replaces either one update the min/max accordingly. Back to [[Chapter 3]] b90103076e29f1189e00ede9449aca4b8200c5f8 3.21 0 102 289 2020-09-20T18:05:08Z Algowikiadmin 1 Created page with "Since all the elements in S2 have keys larger than the keys of the elements in S1, those two trees can be subtrees of one tree whose root node will have a key larger than the..." wikitext text/x-wiki Since all the elements in S2 have keys larger than the keys of the elements in S1, those two trees can be subtrees of one tree whose root node will have a key larger than the largest key in S1 and smaller than the smallest key in S2. Then S2 will be the right subtree and S1 the left subtree. The easiest way to do this is to find the minimum element of S2 (or the maximum of S1) and put it as the root of the concatenated tree and than update its left and right pointer to point to the root of S1 and S2 respectively. The worst-case running time will be O(h), which is the time of retrieving the minimum (maximum) element of a tree. Back to [[Chapter 3]] f1575353c4ccbde3b49b9deac863602cdb7af014 3.23 0 103 290 2020-09-20T18:05:33Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.25 0 104 291 2020-09-20T18:06:01Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.27 0 105 292 2020-09-20T18:06:40Z Algowikiadmin 1 Created page with "Let's put into the black box whole set <math>S=\{x_i\}_{i=1}^n</math>. If <math>bb(S)</math> is True, then such a subset exists and we can go on: # R:=S # for i:=1 to n do ##..." wikitext text/x-wiki Let's put into the black box whole set <math>S=\{x_i\}_{i=1}^n</math>. If <math>bb(S)</math> is True, then such a subset exists and we can go on: # R:=S # for i:=1 to n do ## If <math>bb(R/\{x_i\})</math> is True then <math>R:=R/\{x_i\}</math> When this iteration is finished R will be subset of S that adds up to k. Above solution works even when there are multiple subsets that add up to k. Back to [[Chapter 3]] bd6efded2087a970e9498ac41159e24c8111b8bb 3.29 0 106 293 2020-09-20T18:08:07Z Algowikiadmin 1 Created page with "In each node of a binary tree store the sub tree sum for the left sub tree. #''Add'': while descending the tree to the left update the sub tree sum by adding the value #''Ins..." wikitext text/x-wiki In each node of a binary tree store the sub tree sum for the left sub tree. #''Add'': while descending the tree to the left update the sub tree sum by adding the value #''Insert'': same as add, only now we have to add a new node too. To guaranty <math>O(n \log n)</math> times we can use any balanced tree types, but the balancing step we have to take care of the sub tree sums. For example in AVL trees balancing operations are tree rotations. If P is the left child of Q, and we are doing a right rotation rooted at Q, than the left sub tree of P remains the same, while the left sub tree of Q will now be the original left sub tree of Q minus the left sub tree of P and P itself. The case of a left rotation is similar. Hence the left sub tree sum values can be maintained in <math>O(1)</math> during rotation operations. #''Delete'': When the deleted element has at most 1 child, we only need to update the nodes ancestors. If there are two children, we also need to update elements on the path between the deleted element and it's successor. (The element with which we will swap it before deleting.) #''Partial sum'': With the left sub tree sum values stored in the nodes this operation only involves searching for the element with the specified key, and summing sub tree sums along the way. Back to [[Chapter 3]] 93ed02def117827c40de650ef44a669d5018db28 3.31 0 107 294 2020-09-20T18:08:45Z Algowikiadmin 1 Created page with "Init: k=0 Insert X: k = k+1; A[X] = k; B[k] = X; Search X: return (A[X] < k) and (B[A[X]] == X) Delete X: A[B[k]] = A[X]; B[A[X]] = B[k]; k = k-1; Ple..." wikitext text/x-wiki Init: k=0 Insert X: k = k+1; A[X] = k; B[k] = X; Search X: return (A[X] < k) and (B[A[X]] == X) Delete X: A[B[k]] = A[X]; B[A[X]] = B[k]; k = k-1; Please note that this data structure doesn't support inserting the same X more than once. Back to [[Chapter 3]] da848a1d980c8f20c5f4d51e6e863d1c4fdc8462 3.33 0 108 295 2020-09-20T18:09:38Z Algowikiadmin 1 Created page with " Back to [[Chapter 3]]" wikitext text/x-wiki Back to [[Chapter 3]] d11c620261c0156a4bfcd43bcffd8c59aec1027b 3.35 0 109 296 2020-09-20T18:10:45Z Algowikiadmin 1 Created page with "Sort the shirts by color and do a binary search on color. Back to [[Chapter 3]]" wikitext text/x-wiki Sort the shirts by color and do a binary search on color. Back to [[Chapter 3]] 171231b93c6aac260146c114aadbaf02d07815c6 3.37 0 110 298 2020-09-20T18:12:55Z Algowikiadmin 1 Created page with "<pre> bool compare(struct node* a, struct node* b) { // 1. both empty -> true if (a==NULL && b==NULL) return(true); // 2. both non-empty -> compare them else if (a!..." wikitext text/x-wiki <pre> bool compare(struct node* a, struct node* b) { // 1. both empty -> true if (a==NULL && b==NULL) return(true); // 2. both non-empty -> compare them else if (a!=NULL && b!=NULL) { return( a->data == b->data && compare(a->left, b->left) && compare(a->right, b->right) ); } // 3. one empty, one not -> false else return(false); } </pre> --[[User:Max|Max]] 06:41, 25 June 2010 (EDT) Back to [[Chapter 3]] 59d397a9066ae487c7111993bcc50060bcb6726d 3.39 0 111 299 2020-09-20T18:14:10Z Algowikiadmin 1 Created page with "== Iterative version with three variables == <pre> Node* ReverseList( Node * List ) { Node *temp1 = *List; Node * temp2 = NULL; Node * temp3 = NULL; while ( temp..." wikitext text/x-wiki == Iterative version with three variables == <pre> Node* ReverseList( Node * List ) { Node *temp1 = *List; Node * temp2 = NULL; Node * temp3 = NULL; while ( temp1 ) { *List = temp1; //set the head to last node temp2= temp1->pNext; // save the next ptr in temp2 temp1->pNext = temp3; // change next to privous temp3 = temp1; temp1 = temp2; } return *List; } </pre > == Iterative version with one variable == <pre> Node* reverse (node* head) { node *temp=NULL; while(true) { if(head->next==NULL) { head->next=temp; break; } // Swap(head->next, temp) head->next = (node *)((unsigned long) head->next ^(unsigned long) temp); temp = (node *)((unsigned long) head->next ^(unsigned long) temp); head->next =(node *) ((unsigned long) head->next ^ (unsigned long)temp); // Swap(head, temp) head = (node *)((unsigned long) head ^(unsigned long) temp); temp =(node *) ((unsigned long) head ^(unsigned long) temp); head =(node *) ((unsigned long) head ^ (unsigned long)temp); } } </pre> == Recursive Version == <pre> Node* Reverse(Node*head) { Node* temp=NULL; if(head==NULL) { return NULL; } if(head->next==NULL) return head; temp=Reverse(head->next); head->next->next=head; head->next=NULL; return temp; } </pre> --[[User:Max|Max]] 07:36, 16 June 2010 (EDT) == Recursive Version 2 (Java) == <pre> void reverseList(){ head.reverse(null); Node temp=head; head=tail; tail=temp; } public void reverse(Node previousNode){ if(next!=null) next.reverse(this); next=previousNode; } </pre> == Recursive Version 3 (C++) == <pre> struct node{ int data; node* next; }; node* new_head; // store the new head after calling reverse_sll function void reverse_sll(node* current_node, node* prev_node=NULL){ if(current_node==NULL) new_head = prev_node; else{ reverse_sll(current_node->next, current_node); current_node->next=prev_node; } } </pre> Back to [[Chapter 3]] 067371b4a22cc533f0f638730c64412bdd1889d1 3.41 0 112 300 2020-09-20T18:14:52Z Algowikiadmin 1 Created page with "Create a count sort in magazine until you find all the characters of given string. --[[User:Max|Max]] 06:49, 25 June 2010 (EDT) Back to [[Chapter 3]]" wikitext text/x-wiki Create a count sort in magazine until you find all the characters of given string. --[[User:Max|Max]] 06:49, 25 June 2010 (EDT) Back to [[Chapter 3]] cb904c4ac24fe7b6e0b1e916ca22cfe2ee068192 3.43 0 113 301 2020-09-20T18:16:43Z Algowikiadmin 1 Created page with "<pre> 1) Take two pointers Fast and Slow (Slow jumps one step at a time while Fast two step) 2) While (Fast != NULL) if(Fast == Slow) return true;..." wikitext text/x-wiki <pre> 1) Take two pointers Fast and Slow (Slow jumps one step at a time while Fast two step) 2) While (Fast != NULL) if(Fast == Slow) return true; else fast = fast->next->next slow = slow->next 3) return false; After detecting loop stop one pointer and increment other pointer by 1 step untill Loop if (fast == slow) break; else slow++ LoopLength++ EndLoop You can find out loop start position also. Once you have LoopLenght. 1. Fast = Slow = Start 2. Fast += LoopLenght 3. Loop If Fast != Slow Fast++; Slow++ EndLoop 4. Fast and Slow Both are pointing to the start of loop position. Now you can break the Loop in link list. </pre> --[[User:Max|Max]] 06:18, 25 June 2010 (EDT) Back to [[Chapter 3]] b6d8369ec214cff4e5fa741d69e28f752f37eb48 3.45 0 114 302 2020-09-20T18:18:11Z Algowikiadmin 1 Created page with " == Using a Hashtable == Scan the web page word by word and for each one, except the first, take the pair formed by the previous word + the current one. Use that pair as the..." wikitext text/x-wiki == Using a Hashtable == Scan the web page word by word and for each one, except the first, take the pair formed by the previous word + the current one. Use that pair as the key to a hashtable, where you store the frequency of that pair. If the entry is not already in the hash table store it with value 1, if it is there increment the current value by one. Keep the highest frequency seen and update it wherever you see a greater one. In the end return the greatest frequency seen. Complexity, where N = number of words in the text: Time: O(N) (you only iterate over each word once) Space: O(N) (Worst case is when every word is distinct and thus you have no repeated pairs, and that would take N storage on the hash table) == Using a Trie == The solution with the hashtable will only work in time <math>O(n)</math> if the hashtable contains <math>m=n</math> slots (in general, lookup in a hashtable takes <math>O(n/m)</math> time). Still, this approach could work well if <math>m</math> is chosen wisely. Another approach would be to store the text in a trie and save the number of occurrences in the leaf nodes. The implementation below iterates through the text letter by letter (in <math>O(n)</math> time), using two word pointers <code>word1</code> and <code>word2</code> that point to the position in the trie of the current word and the concatenation of the current with the last word, respectively. If the text is long and natural, it is expected to contain many repetitions and common prefixes, and an efficient implementation would use relatively little memory. Another advantage is that no parameters have to be pre-defined (such as <math>m</math> in the example using the hashtable). <source lang="python"> class Trie: def __init__(self, parent=None): self.parent = parent self.children = {} self.n = 0 def add(self, c): return self.children.setdefault(c, Trie(self)) def letter(self, child): for c in self.children: if self.children[c] == child: return c return None def spell(self): if self.parent: return self.parent.spell() + self.parent.letter(self) return '' def wrap(text): """iterates through text, yielding lowercase letter or single ' '""" inword = False for c in text: c = c.lower() if ord(c) >= ord('a') and ord(c) <= ord('z'): yield c inword = True else: if inword: yield ' ' inword = False if inword: yield ' ' def pairmax(text): """finds the word pair with maximum occurrence""" root = word2 = Trie() best = word1 = None for c in wrap(text): if c == ' ': if word1: word2.n += 1 if best is None or word2.n > best.n: best = word2 word2 = word1.add(' ') word1 = root else: word1 = root word2 = word2.add(' ') else: word2 = word2.add(c) if word1: word1 = word1.add(c) return best.spell(), best.n if __name__ == '__main__': import urllib2, sys urls = sys.argv[1:] if not urls: urls = ['http://www.rfc-editor.org/rfc/rfc19%02d.txt' % i for i in range(30, 40)] for url in urls: word, n = pairmax(urllib2.urlopen(url).read()) sys.stdout.write('%s : %dx "%s"\n' %(url, n, word)) </source> Back to [[Chapter 3]] f0bf1d963ea152eb5d5c12f99b5a2cbfd0f7be22 4.1 0 115 303 2020-09-20T18:20:00Z Algowikiadmin 1 Created page with "Sort it with your favorite nlogn sorting algo. The bottom half is one team, the top half the other. Or much better , partition it with median as pivot . Time complexity O(n)..." wikitext text/x-wiki Sort it with your favorite nlogn sorting algo. The bottom half is one team, the top half the other. Or much better , partition it with median as pivot . Time complexity O(n). Back to [[Chapter 3]] 351725d0e50bf152b402c2eb43954046c8ac4a2c 304 303 2020-09-20T18:20:25Z Algowikiadmin 1 wikitext text/x-wiki Sort it with your favorite nlogn sorting algorithm. The bottom half is one team, the top half the other. Or much better, partition it with median as pivot. Time complexity O(n). Back to [[Chapter 4]] a6097b21e077237ed16e34401ce620642a764831 4.3 0 116 305 2020-09-20T18:20:58Z Algowikiadmin 1 Created page with "<pre> Algo - If we create pair of (min1, max2n) (min1, max2n-1)... will provide optimal result 1) Sort the set of 2n element (n log n) 2) Now assign two pointers Start: A[..." wikitext text/x-wiki <pre> Algo - If we create pair of (min1, max2n) (min1, max2n-1)... will provide optimal result 1) Sort the set of 2n element (n log n) 2) Now assign two pointers Start: A[0] End: A[2n-1] while (start < end) pair(start, end) start++ end-- EndLoop </pre> --[[User:Max|Max]] 06:55, 25 June 2010 (EDT) Back to [[Chapter 4]] 53363882def04de99a41aa1aa7e9834c725cfa8b 4.5 0 117 306 2020-09-20T18:21:48Z Algowikiadmin 1 Created page with "O(nlogn) solution: : sort the array first, : scan the array, keep updating a max_so_far counter. O(n) solution: : put each value into hash map with the value as key and fre..." wikitext text/x-wiki O(nlogn) solution: : sort the array first, : scan the array, keep updating a max_so_far counter. O(n) solution: : put each value into hash map with the value as key and frequency as value : scan the hash map to find the max Back to [[Chapter 4]] 69240efcd2aab7e451d833034fd9b3bf305e1047 4.7 0 118 307 2020-09-20T18:22:24Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.9 0 119 308 2020-09-20T18:23:01Z Algowikiadmin 1 Created page with "Here the main thing to notice is that we need a O(<math>n^{k-1}logn</math>) solution. <br /> For various values of k, <br /> k Solution Time Complexity <br /> 1 O..." wikitext text/x-wiki Here the main thing to notice is that we need a O(<math>n^{k-1}logn</math>) solution. <br /> For various values of k, <br /> k Solution Time Complexity <br /> 1 O(<math>n^0logn</math>) <br /> 2 O(<math>n^1logn</math>) <br /> 3 O(<math>n^2logn</math>) <br /> 4 O(<math>n^3logn</math>) <br /> for k = 2 onwards <br /> 1. sort the array ( nlogn ) <br /> 2. use k-1 loops for iterating over array, where ith loop starts from i-1 + 1 element of array, and then use binary search <br /> eg: <br /> for k = 3 <br /> for( i = 0; i < n; i++ ) <br /> for( j = i+1; j < n; j++ ) <br /> # now use binary search to find ( T - a[i] - a[j] ) from j+1 element to end of array <br /> <h3>Another recursive solution</h3> First, we note that [[ TADM2E 4.8 | an O(<math>n \lg n</math>) solution for <math>k = 2</math> exists ]], which is already within the required bounds. Now, for <math>k \ge 3</math>, we can do something like this: <pre> sort S ascending; CheckSumOfK(S, k, T); // defined below // S must be aorted array of integers function CheckSumOfK(S, k, T) if k <= 2: Use any method in the solution of ex 4.8 and return the result. This can be done in O(n) time because S is sorted. Initialize array A of n - 1 elements; for i from 0 to n - 1: k = 0 // Collect in A all numbers in S but S[i]. Note that A will still be sorted. for j from 0 to n - 1: if i != j: A[k] = S[j]; k = k + 1 // If S[i] is included in the k integers that sum up to T, then there must // exactly (k - 1) integers in the rest of S (i.e., A) that sum to (T - S[i]). // We can find that out by calling ourselves recursively. if CheckSumOfK(A, k - 1, T - S[i]): return True return False </pre> <h3>Complexity</h3> For each item in S, there is <math>O(n)</math> work done in assembling the array A, except at the <math>{k - 1}^{th}</math> recursive call, which completes in <math>O(n \lg n)</math> time. So, for each number in S, we have <math>O(kn) + O(n \lg n)</math> work, and since <math>k <= n</math>, each iteration of the outer loop takes <math>O(n^2) + O(n \lg n) = O(n^2)</math> work. Now since the outer loop goes on for at most <math>n</math> iterations, we have a total runtime of <math>O(n^3)</math>. A trick can be used to lower this runtime to <math>O(n^2 \lg n)</math> by having the routines in [[ TADM2E 4.8 | this solution ]] take an index to ignore when iterating in their inner loops. With this, we save the <math>O(n)</math> construction of A for every item, and then each iteration of the outer loop becomes <math>O(n \lg n)</math> (How? <math>O(k) = O(n)</math> constant time recursive calls plus <math>O(n \lg n)</math> time spent in the <math>{k - 1}^{th}</math> calls), giving a total runtime of <math>O(n^2 \lg n)</math> for <math>n</math> elements in S. Note that this cost includes the initial <math>O(n \lg n)</math> cost for sorting S. Also, this algorithm is always going to be <math>O(n^{k-1} \lg n)</math> for all <math>k >= 2</math>. The exercise just states an upper bound. Back to [[Chapter 4]] f64901b17bcc38dad24c9a80276dc022795a9cd4 309 308 2020-09-20T18:23:24Z Algowikiadmin 1 wikitext text/x-wiki Here the main thing to notice is that we need a O(<math>n^{k-1}logn</math>) solution. <br /> For various values of k, <br /> k Solution Time Complexity <br /> 1 O(<math>n^0logn</math>) <br /> 2 O(<math>n^1logn</math>) <br /> 3 O(<math>n^2logn</math>) <br /> 4 O(<math>n^3logn</math>) <br /> for k = 2 onwards <br /> 1. sort the array ( nlogn ) <br /> 2. use k-1 loops for iterating over array, where ith loop starts from i-1 + 1 element of array, and then use binary search <br /> eg: <br /> for k = 3 <br /> for( i = 0; i < n; i++ ) <br /> for( j = i+1; j < n; j++ ) <br /> # now use binary search to find ( T - a[i] - a[j] ) from j+1 element to end of array <br /> <h3>Another recursive solution</h3> First, we note that an O(<math>n \lg n</math>) solution for <math>k = 2</math> exists, which is already within the required bounds. Now, for <math>k \ge 3</math>, we can do something like this: <pre> sort S ascending; CheckSumOfK(S, k, T); // defined below // S must be aorted array of integers function CheckSumOfK(S, k, T) if k <= 2: Use any method in the solution of ex 4.8 and return the result. This can be done in O(n) time because S is sorted. Initialize array A of n - 1 elements; for i from 0 to n - 1: k = 0 // Collect in A all numbers in S but S[i]. Note that A will still be sorted. for j from 0 to n - 1: if i != j: A[k] = S[j]; k = k + 1 // If S[i] is included in the k integers that sum up to T, then there must // exactly (k - 1) integers in the rest of S (i.e., A) that sum to (T - S[i]). // We can find that out by calling ourselves recursively. if CheckSumOfK(A, k - 1, T - S[i]): return True return False </pre> <h3>Complexity</h3> For each item in S, there is <math>O(n)</math> work done in assembling the array A, except at the <math>{k - 1}^{th}</math> recursive call, which completes in <math>O(n \lg n)</math> time. So, for each number in S, we have <math>O(kn) + O(n \lg n)</math> work, and since <math>k <= n</math>, each iteration of the outer loop takes <math>O(n^2) + O(n \lg n) = O(n^2)</math> work. Now since the outer loop goes on for at most <math>n</math> iterations, we have a total runtime of <math>O(n^3)</math>. A trick can be used to lower this runtime to <math>O(n^2 \lg n)</math> by having the routines in [[ TADM2E 4.8 | this solution ]] take an index to ignore when iterating in their inner loops. With this, we save the <math>O(n)</math> construction of A for every item, and then each iteration of the outer loop becomes <math>O(n \lg n)</math> (How? <math>O(k) = O(n)</math> constant time recursive calls plus <math>O(n \lg n)</math> time spent in the <math>{k - 1}^{th}</math> calls), giving a total runtime of <math>O(n^2 \lg n)</math> for <math>n</math> elements in S. Note that this cost includes the initial <math>O(n \lg n)</math> cost for sorting S. Also, this algorithm is always going to be <math>O(n^{k-1} \lg n)</math> for all <math>k >= 2</math>. The exercise just states an upper bound. Back to [[Chapter 4]] 41baad4533ad753fd88244c2ee9708232f2c3809 4.11 0 120 310 2020-09-20T18:24:04Z Algowikiadmin 1 Created page with "Note that there can be at most one element which appears more than n/2 (i.e. at least ceil(n/2) ) times in the list. If we now consider the *sorted* version of the list, we'l..." wikitext text/x-wiki Note that there can be at most one element which appears more than n/2 (i.e. at least ceil(n/2) ) times in the list. If we now consider the *sorted* version of the list, we'll see that if such an element exists, it covers a consecutive range in the list. The start of this range can be at various places with the extreme cases being: * the range starts at element 1 * the range ends at element n We can easily see that in both cases (and therefore also in the cases between the extremes) that if there is an element which appears more than n/2 times, the element at position ceil(n/2) (in the sorted list) must have this value. This leads to the following algorithm: Determine the ceil(n/2) smallest element of the (unsorted) set. The [http://en.wikipedia.org/wiki/Selection_algorithm#Linear_general_selection_algorithm_-_Median_of_Medians_algorithm BFPRT] algorithm can do that in O(n) running time in the worst case . Go through the list element by element and count the number of occurrences of this ceil(n/2) smallest element. If it occurs at more than n/2 times, this is the element we were looking for, otherwise the list does not contain such an element. ---- Alternatively, successively remove pairs of distinct elements from the list. If there is an element that appears more than n/2 times, it won't have a pair eventually. The algorithm below removes pairs of distinct elements from the list until the residual list (maybe empty) contains only equal elements. If the residual list is non-empty, its head is a candidate for the element that appears more than n/2 times. Whether it is so can be checked in one linear sweep. If the residual list is empty, the list doesn't contain an element that appears more than n/2 times. The algorithm works as follows. We traverse the list recursively and maintain an additional stack. The stack is initially empty. If both the list and stack are non-empty, then either the top of the stack is distinct from the head of the list, in which case they both can be removed, or they are equal, in which case we move the head of the list to the top of the stack, thus maintaining the invariant that the stack contains only equal elements. If the stack becomes empty, but there are still elements in the list, we move the head of the list onto the stack. If the list becomes empty, the stack contains elements for which there was no pair. We can implement this algorithm in Haskell as follows: candidates :: Eq a => [a] -> [a] candidates = go [] where go ys [] = ys go [] (x:xs) = go [x] xs go (y:ys) (x:xs) | y == x = go (x:y:ys) xs | y /= x = go ys xs ---- == Algorithm for any arbitrary number of occurrences == For each element insert it into a dictionary. If the element is not in the dictionary add it with a count of one, if it is already in the dictionary increment its value. This can be done in linear time and space if using something like a hash function. Each time we change the count of an element we check if its frequency exceeds the number we are looking for. If it does we either save it or return it, depending on whether we want to find one or all such elements. Back to [[Chapter 4]] 4bda3e50321b97fdee3d99e4bf4ce60c9afb71c1 4.13 0 121 311 2020-09-20T18:25:03Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.15 0 122 312 2020-09-20T18:25:06Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.17 0 123 313 2020-09-20T18:25:52Z Algowikiadmin 1 Created page with "Scan through the array to build an out-of-order heap, that is, the first element is indeed the smallest, but necessarily any of the other elements. This takes us O(n). Then ex..." wikitext text/x-wiki Scan through the array to build an out-of-order heap, that is, the first element is indeed the smallest, but necessarily any of the other elements. This takes us O(n). Then extract from the heap k times to get the smallest k elements using O(klogn). Thus the total time is O(n+klogn). Other solution: Create a heap using bubble down per 4.3.4 in the book. This takes O(n) time. Then, extract from the heap in k times to get the smallest k elements. This takes O(k*logn) time. Therefore, total time is O(n + k*logn) Back to [[Chapter 4]] 132be8ebe69f9865d6f5e24d1bc6f2baeafc1cee 4.19 0 124 314 2020-09-20T18:26:26Z Algowikiadmin 1 Created page with "1) Finding the maximum element is O(1) in both a max-heap (the root of the heap) and a sorted array (the last element in the array), so for this operation, both data structure..." wikitext text/x-wiki 1) Finding the maximum element is O(1) in both a max-heap (the root of the heap) and a sorted array (the last element in the array), so for this operation, both data structures are equally optimal. (The max-heap is ''marginally'' faster, since the array length doesn't need to be accessed, but this splits hairs.) 2) Assuming the index of the element is known, a deletion on a heap costs O(log n) time to bubble down. A sorted array requires all elements to be updated leading to a O(n) operation. 3) A heap can be formed in O(n) time. The sorted array will require a sort costing O(n log n). 4) Finding the minimum element in a max-heap requires visiting each of the leaf nodes in the worst case, i.e. is an O(n) operation. Finding the minimum element in a sorted array is an O(1) operation (it's the first element), so the sorted array performs (asymptotically) better. Back to [[Chapter 4]] 1a9fc68855ed68ce0d987845702eb3d03d62f303 4.21 0 125 315 2020-09-20T18:27:20Z Algowikiadmin 1 Created page with "the general form of this problem is to find the kth largest value. finding the median is when k = n/2. to find the kth largest value, * select a partition element and split..." wikitext text/x-wiki the general form of this problem is to find the kth largest value. finding the median is when k = n/2. to find the kth largest value, * select a partition element and split the array into 2 sub-arrays - one with the elements smaller than the partition and one with the elements larger than the partition. '''O(n)''' * if the array with the elements larger than the partition has k - 1 elements, the partition is the kth largest element * if the array with the elements larger than the partition has >= k elements, recurse with the same value of k using the larger elements as the new array. '''O(n/2)''' (average case) * else the median is in the array with elements smaller than the partition so adjust k to account for the large elements being discarded and recurse using the smaller elements as the new array '''O(n/2)''' (average case) the overall complexity is O(n) since O(n) + O(n/2) + O(n/4) + ... approaches O(2n) which is just O(n) since 2 is a constant. Back to [[Chapter 4]] 4e83305529794bac3de081bcf12e765282def20b 4.23 0 126 316 2020-09-20T18:28:01Z Algowikiadmin 1 Created page with "<pre> from random import choices def group_by_color(data, start, stop, color): operations_counter = 0 while start < stop and start < len(data) and stop >= 0:..." wikitext text/x-wiki <pre> from random import choices def group_by_color(data, start, stop, color): operations_counter = 0 while start < stop and start < len(data) and stop >= 0: # data[start] is equal to examine(data[start]) except swap function call while start < len(data) and data[start] == color: # Finding first element with incorrect color from start start += 1 # Element with correct color already on right position, moving forward operations_counter += 1 while start < stop and stop >= 0 and data[stop] != color: # Finding first element with correct color from end stop -= 1 # Element with incorrect color will be swapped later or keep on that position, moving backward operations_counter += 1 if start < stop: data[start], data[stop] = data[stop], data[start] # Equal to swap(start, stop) operations_counter += 1 assert operations_counter <= len(data) * 2 # Check O(2*n) restriction return start # Return last element with specified color colors_list = choices(['red', 'white', 'blue'], k=100) red_ending = group_by_color(colors_list, 0, len(colors_list) - 1, 'red') # Sort red O(n) group_by_color(colors_list, red_ending + 1, len(colors_list) - 1, 'white') # Sort white O(n) # Blue already sorted </pre> First pass: group red elements from 0 to k. Swapping not red elements found from begin and red elements found from end. Second pass: group white elements from k to j. Swapping not white elements from k and white elements from end. Blue elements already grouped from j to n. --[[User:Bkarpov96|Bkarpov96]] ([[User talk:Bkarpov96|talk]]) 17:36, 19 June 2020 (UTC) Back to [[Chapter 4]] a4026d2c619b6c863ac7a48ca88a347c80510aa0 4.25 0 127 317 2020-09-20T18:28:31Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.27 0 128 318 2020-09-20T18:28:40Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.29 0 129 319 2020-09-20T18:28:54Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.31 0 130 320 2020-09-20T18:29:31Z Algowikiadmin 1 Created page with "To guarantee mergesort is stable, when merging the two subarrays together, mergesort should settle ties in the lists by choosing the lower indexed value. Back to [[Chapter 4]]" wikitext text/x-wiki To guarantee mergesort is stable, when merging the two subarrays together, mergesort should settle ties in the lists by choosing the lower indexed value. Back to [[Chapter 4]] c6ab4ccaf92e31aad258b7df8f5ffcb248962889 4.33 0 131 321 2020-09-20T18:30:06Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.35 0 132 322 2020-09-20T18:30:31Z Algowikiadmin 1 Created page with "It's not clear to me if the first <math>n - \sqrt n</math> element being sorted means that the remaining <math>\sqrt n</math> elements are all bigger or not, but let's suppose..." wikitext text/x-wiki It's not clear to me if the first <math>n - \sqrt n</math> element being sorted means that the remaining <math>\sqrt n</math> elements are all bigger or not, but let's suppose they are not, since that's more general. First sort the remaining <math>\sqrt n</math> elements in <math>O(\sqrt n \log \sqrt n)</math> time. After that we can do a simple merge step to merge the two sorted arrays in <math>O(n)</math> time. Since <math>\sqrt n</math> dominates <math>\log \sqrt n</math> we can come to the conclusion that the total running time of this algorithm is: <math>O(\sqrt n \log \sqrt n + n) = O(\sqrt n \sqrt n + n) = O(n + n) = O(n)</math> Back to [[Chapter 4]] 241eca8bf49a93a3e056128993130f45b3980735 4.37 0 133 323 2020-09-20T18:30:59Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.39 0 134 324 2020-09-20T18:31:39Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.41 0 135 325 2020-09-20T18:32:17Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.43 0 136 326 2020-09-20T18:32:51Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.45 0 137 327 2020-09-20T18:33:06Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.47 0 138 328 2020-09-20T18:34:09Z Algowikiadmin 1 Created page with " For a known set of integer numbers ( assume Nr-1, Nr-2 ... Nr-k) the best is to use a non-comparison based sort algorithm like radix sort with O(n) You have an array that cr..." wikitext text/x-wiki For a known set of integer numbers ( assume Nr-1, Nr-2 ... Nr-k) the best is to use a non-comparison based sort algorithm like radix sort with O(n) You have an array that creates a histogram of all numbers ( histoThenStartIndexArray[Nr-i]++) Step 2, in the same array calculate the index of that position For example if there are 3 numbers 99, and 5 numbers 105, the next index will be 8 for the next number Step 3, parse array and display values Back to [[Chapter 4]] 2c2cc1a148afeb436f25c5c68b875d0404ad9ba2 4.49 0 139 329 2020-09-20T18:35:35Z Algowikiadmin 1 Created page with " Back to [[Chapter 4]]" wikitext text/x-wiki Back to [[Chapter 4]] d03d9cd5b1151abef641ade4b301bf88c9502196 4.53 0 140 330 2020-09-20T18:36:16Z Algowikiadmin 1 Created page with "If we are allowed to maintain a second stack on the side, this should be possible. The main stack is a regular stack that can be implemented using an array and an index to the..." wikitext text/x-wiki If we are allowed to maintain a second stack on the side, this should be possible. The main stack is a regular stack that can be implemented using an array and an index to the top of the stack. The second stack will stack each entry when it becomes the new minimum, only. When an entry is popped from the main stack, it will be poped from the second stack only if identical. The minimum item is the top of the second stack. I think the actual question was about ability of building the stack that has push/pop/extract-min operations as in the queues - the extract removes element. This is impossible - that would give us an opportunity to sort in O(n) time (n O(1) pushes to the data structure, and then n extract-min's) --[[User:Kubek2k|Kubek2k]] 05:00, 6 November 2011 (EST) Back to [[Chpater 4]] dbea60ad6d63859912500af84065bf816af86362 331 330 2020-09-20T18:36:26Z Algowikiadmin 1 wikitext text/x-wiki If we are allowed to maintain a second stack on the side, this should be possible. The main stack is a regular stack that can be implemented using an array and an index to the top of the stack. The second stack will stack each entry when it becomes the new minimum, only. When an entry is popped from the main stack, it will be poped from the second stack only if identical. The minimum item is the top of the second stack. I think the actual question was about ability of building the stack that has push/pop/extract-min operations as in the queues - the extract removes element. This is impossible - that would give us an opportunity to sort in O(n) time (n O(1) pushes to the data structure, and then n extract-min's) --[[User:Kubek2k|Kubek2k]] 05:00, 6 November 2011 (EST) Back to [[Chapter 4]] 961feea0f88ab157d206b6d348fb04b08ad00f43 333 331 2020-09-20T18:37:29Z Algowikiadmin 1 wikitext text/x-wiki Question: You are given 12 coins. One of them is heavier or lighter than the rest. Identify this coin in just three weighings Solution: Number the coins 1 through 12 and divide them coins into 4 sets of 3... There are multiple comparison sets possible. This is an acceptable template to find a few of them. (This template is NOT definitive, there are other solutions that don't follow this template) Compare (set 1 & 1st coin from set 4) against (set 2 + 2nd coin from set 4) Compare (set 1 & 2nd coin from set 4) against (set 3 + 1st coin from set 4) Compare (1st coin from each set) against (3rd coin from each set) A more concise example: Compare 1 2 3 10 against 4 5 6 11 Compare 1 2 3 11 against 7 8 9 10 Compare 1 4 7 10 against 3 6 9 12 Each weighing can have 3 possible outcomes: Left Heavy, Right Heavy, or Balanced (L,R or B) Build a truth table to interpret outcomes...many outcomes are not possible. Note: THE TABLE VALUES ARE DERIVED FROM THE CHOSEN COMPARISON SETS! outcome: fake coin: l l l 1 is heavy r r r 1 is light l l b 2 is heavy r r b 2 is light l l r 3 is heavy r r l 3 is light r b l 4 is heavy l b r 4 is light r b b 5 is heavy l b b 5 is light r b r 6 is heavy l b l 6 is light b r l 7 is heavy b l r 7 is light b r b 8 is heavy b l b 8 is light b r r 9 is heavy b l l 9 is light r l r 10 is heavy l r l 10 is light l r b 11 is heavy r l b 11 is light b b r 12 is heavy b b l 12 is light There are multiple comparison set possibilities, each with their own comparison table solution. ---- A simpler solution #2: * put 6 coins on each side of the scale, one side will be heavier. * use the heavier side from the first weighing and put 3 coins on each side of the scale. * using the heavier side from the 2nd weighing, pick 2 coins and put 1 on each side of the scale. If the scale is balanced then the coin you didn't weigh is the heavier one. Otherwise, the scale will show which one of the other 2 is the heavy coin. ---- Since we do not know if the faulty coin is heavier or lighter , the soluition #2 is not correct The only solution is solution nr 1, for which we can also use a binary tree ---- I found the explanation at mathforum.org/library/drmath/view/55618.html to be helpful. The key is to build a truth table, but how that table was built is a little tricky because you can't just write out all possible combinations of results and use that. I tried writing a truth table and found that I had to swap some values around to make sure that each measurement step had exactly 4 on each side. Back to [[Chapter 4]] 5bbcfaa35636e65275805d5be90b2d6d0436a7c0 4.51 0 141 332 2020-09-20T18:36:56Z Algowikiadmin 1 Created page with "If we are allowed to maintain a second stack on the side, this should be possible. The main stack is a regular stack that can be implemented using an array and an index to the..." wikitext text/x-wiki If we are allowed to maintain a second stack on the side, this should be possible. The main stack is a regular stack that can be implemented using an array and an index to the top of the stack. The second stack will stack each entry when it becomes the new minimum, only. When an entry is popped from the main stack, it will be poped from the second stack only if identical. The minimum item is the top of the second stack. I think the actual question was about ability of building the stack that has push/pop/extract-min operations as in the queues - the extract removes element. This is impossible - that would give us an opportunity to sort in O(n) time (n O(1) pushes to the data structure, and then n extract-min's) --[[User:Kubek2k|Kubek2k]] 05:00, 6 November 2011 (EST) Back to [[Chapter 4]] 961feea0f88ab157d206b6d348fb04b08ad00f43 5.1 0 142 334 2020-09-21T00:53:44Z Algowikiadmin 1 Created page with "'''Part -1''' Since set is sorted the max element will lie at position <pre> Since set is sorted the max element will lie at position Max = Set[k] where k != 0 Set[n..." wikitext text/x-wiki '''Part -1''' Since set is sorted the max element will lie at position <pre> Since set is sorted the max element will lie at position Max = Set[k] where k != 0 Set[n] where K == n </pre> '''Part -2 ''' Apply binary search to find out transition point <pre> Assume set indexes are zero based FindNumberOfRotations(A): 1. if (A[0] < A[n-1]) then There is no rotation made return 0 2. low = 0, high =1 3. mid = (low + high)/2 4. if(A[mid] < A[high]) then Transition lies in left half of the array if A[mid-1] > A[mid] then return mid else high = mid-1 Go to step 3. else Transition lies in right half of array if(A[mid] > A[mid+1]) then return mid+1 else low = mid+1 Go to step 3 </pre> Back to [[Chapter 5]] ca273a9e86ea74c724aa7fdfc518e1c24cc653df 5.3 0 143 335 2020-09-21T00:54:40Z Algowikiadmin 1 Created page with "(1) Do a binary search within the range of <math>1-n</math>. You guess the right number within O(log n) questions. (2) If you don't know n start with a random number <math>2^..." wikitext text/x-wiki (1) Do a binary search within the range of <math>1-n</math>. You guess the right number within O(log n) questions. (2) If you don't know n start with a random number <math>2^i</math> and if it is larger than the number you are looking for do a binary search within <math>1-2^i</math> as in (1). If <math>2^i</math> is less then guess <math>2^{i+1}</math> and repeat. Back to [[Chapter 5]] 05b566083acf1c6359d70e04fc1312649e63ffb1 5.5 0 144 336 2020-09-21T00:55:41Z Algowikiadmin 1 Created page with "Apply binary search to find out transition point <pre> Assume set indexes are zero based FindIndex(A): 1. low = 0, high =1 2. mid = (low + high)/2 3. if(A[mid] >..." wikitext text/x-wiki Apply binary search to find out transition point <pre> Assume set indexes are zero based FindIndex(A): 1. low = 0, high =1 2. mid = (low + high)/2 3. if(A[mid] > mid) then Index will lie in left half of the array high = mid-1 Go to step 2. else if (A[mid] < mid) then Index will lie in right half of array low = mid + 1 else return mid </pre> --[[User:Max|Max]] 07:31, 25 June 2010 (EDT) Back to [[Chapter 5]] 8045c8c72708b9d1e502b3e50f260b4b9af65594 5.7 0 145 337 2020-09-21T00:56:23Z Algowikiadmin 1 Created page with "<math>O(n+m)</math> is necessary and sufficient. Lower bound comes from potentially independent values along second diagonal -- upper bound comes from observing that we can el..." wikitext text/x-wiki <math>O(n+m)</math> is necessary and sufficient. Lower bound comes from potentially independent values along second diagonal -- upper bound comes from observing that we can eliminate either a row or a column in each comparison if we start from the lower left corner and walk up or left. Having M[0..n-1][0..m-1] and a struct Point {int x, int y}, we could have the following solution: Point* findPosition(int key) { int row = 0, col = m-1; while (row < n && col >= 0) { if (M[row][col] == key) { return new Point(row,col); } else if (M[row][col] > key) { col--; } else row++; } return NULL; } Back to [[Chapter 5]] 0a2d087e9408d1c45813c0bcd3b97f99324a78b4 5.9 0 146 338 2020-09-21T00:56:46Z Algowikiadmin 1 Created page with " Back to [[Chapter 5]]" wikitext text/x-wiki Back to [[Chapter 5]] 35f902bd61abec97043742bd84f3abf62df9b75b 5.11 0 147 339 2020-09-21T00:56:50Z Algowikiadmin 1 Created page with " Back to [[Chapter 5]]" wikitext text/x-wiki Back to [[Chapter 5]] 35f902bd61abec97043742bd84f3abf62df9b75b 5.13 0 148 340 2020-09-21T00:56:54Z Algowikiadmin 1 Created page with " Back to [[Chapter 5]]" wikitext text/x-wiki Back to [[Chapter 5]] 35f902bd61abec97043742bd84f3abf62df9b75b 5.15 0 149 341 2020-09-21T00:56:58Z Algowikiadmin 1 Created page with " Back to [[Chapter 5]]" wikitext text/x-wiki Back to [[Chapter 5]] 35f902bd61abec97043742bd84f3abf62df9b75b 6.3 0 150 342 2020-09-21T00:57:54Z Algowikiadmin 1 Created page with "1) Starting from left to right, the number of inversions for 1st number is n-1 for 2nd number is n-2 ... .. ....nth number is n-n = 0 Total number of inversions is..." wikitext text/x-wiki 1) Starting from left to right, the number of inversions for 1st number is n-1 for 2nd number is n-2 ... .. ....nth number is n-n = 0 Total number of inversions is the sum of all of the above sum of integers from 0 to n - 1 = n(n-1)/2 2. We know that the maximum num of inversions is n(n-1)/2. Consider a permutation P. a1 a2 ai..ak...aj..am.... an-1 an ai and aj are out of order ak and am are in order. For Pr, ai and aj will get reversed, and will become in order. ak and am will get reversed and will become out of order. Every In order pair becomes an inversion. And every inversion becomes corrected. If P contains "x" inversions.. then it also contained "n(n-1)/2 - x" in order pairs. Thus Pr contains "x" in order pairs and "n(n-1)/2 - x" inversions. Summing up the inversions in P and Pr we get n(n-1)/2 3. To calculate the max inversions in part 1, every ith position had n - i inversions. i.e following a number, all other numbers will be out of order. In the average case, we can argue that about 1/2 of the number will be out of order.... for 1st number (n-1)/2 for 2nd number (n-2)/2 ... .. ....nth number is (n-n)/2 = 0 Total number of inversions is the sum of all of the above sum of integers from 0 to (n - 1)/2 = n(n-1)/4... '''Alternative solution to 3:''' Any permutation of any set is equally likely. Previous result shows that every permutation has its reversal. If we put pair up every permutation of a set with its reversal, they combine to n(n-1)/2 inversions. There are half as much pairs as there are permutations so the result is n(n-1)/2 * 1/2 = n(n-1)/4 Back to [[Chapter 6]] 927cca1a3fdda33f310f796dab0d2d0081db9033 6.1 0 151 343 2020-09-21T00:58:37Z Algowikiadmin 1 Created page with " Back to [[Chapter 6]]" wikitext text/x-wiki Back to [[Chapter 6]] 207d2c376ff3038ce97436bbf30b4fb406b38bb4 6.5 0 152 344 2020-09-21T00:58:41Z Algowikiadmin 1 Created page with " Back to [[Chapter 6]]" wikitext text/x-wiki Back to [[Chapter 6]] 207d2c376ff3038ce97436bbf30b4fb406b38bb4 6.7 0 153 345 2020-09-21T00:58:45Z Algowikiadmin 1 Created page with " Back to [[Chapter 6]]" wikitext text/x-wiki Back to [[Chapter 6]] 207d2c376ff3038ce97436bbf30b4fb406b38bb4 6.9 0 154 346 2020-09-21T00:58:48Z Algowikiadmin 1 Created page with " Back to [[Chapter 6]]" wikitext text/x-wiki Back to [[Chapter 6]] 207d2c376ff3038ce97436bbf30b4fb406b38bb4 6.11 0 155 347 2020-09-21T00:58:52Z Algowikiadmin 1 Created page with " Back to [[Chapter 6]]" wikitext text/x-wiki Back to [[Chapter 6]] 207d2c376ff3038ce97436bbf30b4fb406b38bb4 7.1 0 156 348 2020-09-21T00:59:55Z Algowikiadmin 1 Created page with "(a) BFS: * Graph G1: A, B, D, I, C, E, G, J, F, H * Graph G2: A, B, E, C, F, I, D, G, J, M, H, K, N, L, O, P (b) DFS: * Graph G1: A, B, C, E, D, G, H, F, J, I * Graph G2: A,..." wikitext text/x-wiki (a) BFS: * Graph G1: A, B, D, I, C, E, G, J, F, H * Graph G2: A, B, E, C, F, I, D, G, J, M, H, K, N, L, O, P (b) DFS: * Graph G1: A, B, C, E, D, G, H, F, J, I * Graph G2: A, B, C, D, H, G, F, E, I, J, K, L, P, O, N, M Back to [[Chapter 7]] fdee7bd9a9201d97a953d0dda24c3d1b5cf0c252 7.3 0 157 349 2020-09-21T01:00:54Z Algowikiadmin 1 Created page with "Induction proof: Base case: Tree composed of just two nodes: x(root) and y. There is only one way x -> y Assuming there is an unique path between x and y, we add a new leaf..." wikitext text/x-wiki Induction proof: Base case: Tree composed of just two nodes: x(root) and y. There is only one way x -> y Assuming there is an unique path between x and y, we add a new leaf z under y. z is only connected to its parent y, so there is only one way from z to x that is z -> y->[unique path assumed]->x Back to [[Chapter 7]] ed3cce19668ddee57510468c144f415c5d7a0446 7.5 0 158 350 2020-09-21T01:01:23Z Algowikiadmin 1 Created page with "Graphs with max degree 2, can be bipartite (even number of edges) or tripartite (odd number of edges) ----- Consider a triangle (3 edges, 3 vertices): it's not bipartite eve..." wikitext text/x-wiki Graphs with max degree 2, can be bipartite (even number of edges) or tripartite (odd number of edges) ----- Consider a triangle (3 edges, 3 vertices): it's not bipartite even though every node has an even number of edges and the graph has an even number of edges. ----- For such a graph, such that the degree of every vertex is at most 2, we can use a DFS traversal, coloring the child the opposite color of the parent. When we see a Back edge, we color the currently discovered child with a color different that the parent, and also different from the ancestor discovered through that back edge. Since there is just one traversal, it runs in O(m + n) (m edges, n vertices) ----- Another Solution: A graph whose each vertex has degree at most K has chromatic number less than equal to K + 1. It is given that each degree has vertex at most 2, therefore determine whether the graph is two colourable or not with the help of BFS traversal. If it is then the answer is 2 otherwise the answer is 3. If graph has only one vertex then the answer will be 1. Let me know whether the solution is right? (sah.sslpu@gmail.com) BTW it is right! Back to [[Chapter 7]] 5d8e7b65b8d333de70bb90e5f10d9254683ec74c 7.7 0 159 351 2020-09-21T01:01:54Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.9 0 160 352 2020-09-21T01:02:23Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.11 0 161 353 2020-09-21T01:02:54Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.13 0 162 354 2020-09-21T01:03:09Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.15 0 163 355 2020-09-21T01:03:13Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.17 0 164 356 2020-09-21T01:04:21Z Algowikiadmin 1 Created page with "1) We can determine that leafs should never be included into the cover. Therefore all leaves should be unmarked, which means that all of their parents should be marked. Now we..." wikitext text/x-wiki 1) We can determine that leafs should never be included into the cover. Therefore all leaves should be unmarked, which means that all of their parents should be marked. Now we remove all leaves from the tree and all of their parents, together with all of their edges. We then repeat this process on the modified tree. 2) We consider again the leafs, each is degree 1. For each leaf, if we consider its parent, its degree is the number of children + 1. It means that we have to choose between removing the n children of degree one or the parent of degree n+1. We remove the parent, mark the leafs as included, and recurse as in 1) above. 3) We know we will be able to remove at most one every other node, so we use a two-coloring technique (Red/Black) and perform a post-order traversal. Let's assume we will remove all the Black node. When we process a node, we also store with each node the sum over its immediate children of the respective Red and Black weight for the subtree. If not all of the children are Red, we need to mark the current node as Red. But we also have the option to reverse the coloring of all the Red-children's subtree. So we look at the sum over the red-children for Red and Black, and compare the difference of these sum to the current node's weight. If the current node's weight is above, we swap the coloring for these subtree. The current node will record the Black and Red sum of its children's subtree, and add its own weight to its color. Back to [[Chapter 7]] a63927f8300ed806c86959d72607c2d3d674277e 7.19 0 165 357 2020-09-21T01:05:34Z Algowikiadmin 1 Created page with "# This translates to the question of labeling the tree with two colors, because this way each edge's vertices are colored differently. The larger group of colors is the sought..." wikitext text/x-wiki # This translates to the question of labeling the tree with two colors, because this way each edge's vertices are colored differently. The larger group of colors is the sought for maximum-size independent set. Back to [[Chapter 7]] 97dcc895a8cab2420fc4e4aa826919ceb181b2bc Chapter 7 0 10 358 247 2020-09-21T01:06:06Z Algowikiadmin 1 /* Algorithm Design */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38. An articulation vertex of a connected graph <math>G</math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) [[7.39|Solution]] :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. [[7.41|Solution]] ===Interview Problems=== :7.42. Which data structures are used in depth-first and breath-first search? :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order. [[7.43|Solution]] Back to [[Chapter List]] 9f4c96c52d5502139b2816415e3c970dd181b035 7.21 0 166 359 2020-09-21T01:06:57Z Algowikiadmin 1 Created page with "(a) Compare every possible set of three vertices and test if there is an edge between the three. (b) One may be tempted to use DFS to find cycle of length 3, by maintaining a..." wikitext text/x-wiki (a) Compare every possible set of three vertices and test if there is an edge between the three. (b) One may be tempted to use DFS to find cycle of length 3, by maintaining an array parent[I] and check any backedge u-v whether grandparent of u is equal to v. However this is incorrect, consider the graph A - B - C - D - A (square) D - E - F - G - D (another square, connected to the first one through D) A - G (edge that closes the triangle A - D - G). DFS would produce: A -> B - > C -> D (grandParent of D is B) -> A (nothing to do, back up the recursion to D and move to the next edge) -> E -> F -> G (grandparent of G is E) At this point we find a cycle when considering the edge G - D, but the grandparent of D isn't connected to G. So we continue and consider A, and then the recursion ends and we process back every node till A, and consider the last edge: A -G. Since A isn't connected to the grandfather of G, we don't find the triangle. Some possible solutions can be found in : http :// i11www.iti.uni-karlsruhe.de/extra/publications/sw-fclt-05_t.pdf ---- Alternative solution <pre> 1. O(|V|^3) - If only adjacency lists are available, convert adjacency lists to the adjacency matrix - If an adjacency matrix is available, traverse the matrix O(|V|^2) get a vertex U and an adjacent M, traverse the M row O(|V|) and check the vertices for adjacency with U O(1) 2. O(|V|*|Е|), |V| <= |E| - Data preparation - - Let's analyze the case with the largest upper bound => assume that |V| = |E| - - If an adjacency matrix is available, create additional adjacency lists O(|V|^2) - - If adjacency lists are given, create an additional adjacency matrix O(|V| + |E|) = O(2 * |V|) by assumption ~ O(|V|) - - At this stage, we have adjacency lists and an adjacency matrix, and each element of the adjacency lists also stores its coordinates from the adjacency matrix - Algorithm - - Traverse adjacency lists O(|V| + |E|) vertex U is the owner of the current list, vertex M is a list item - - - Traversing a list owned by M O(|V|) vertex M is the owner of the list, vertex C is a list item - - - - Checking C for adjacency with U O(1), since the coordinates of the vertices are known => row U and column C are known - - Time complexity O(|V| + |E|) * O(|V|) * O(1) ~ O(|V|) * O(|V|) = O(|V|^2) ~ O(|V| * |E|), by assumption |V| = |E| </pre> --[[User:Bkarpov96|Bkarpov96]] ([[User talk:Bkarpov96|talk]]) 10:16, 5 July 2020 (UTC) Back to [[Chapter 7]] 70973279bf80d9ad467aec388d9b8ea528ac9d7f 7.23 0 167 360 2020-09-21T01:07:57Z Algowikiadmin 1 Created page with " for any node in the tree, there are two possibilities # either the diameter is contained in one of the subtrees # or the node itself is at the top of the longest path in the..." wikitext text/x-wiki for any node in the tree, there are two possibilities # either the diameter is contained in one of the subtrees # or the node itself is at the top of the longest path in the tree that defines the diameter in the second case, the diameter is equal to the sum of depth of the two deepest sub-trees of that node the following algorithm calculates the diameter in <math>O(n)</math> time <source lang="python"> class Node: def __init__(self, value, children): self.value = value self.children = children @classmethod def preorder(cls, lists): return cls( lists[0], [cls.preorder(l) for l in lists[1:]] ) def dd(root): """ returns depth, diameter for tree with given root node >>> dd(Node.preorder([0, [1], [2]])) (1, 2) >>> dd(Node.preorder([1, [2, [3]], [4, [5]]])) (2, 4) >>> dd(Node.preorder([1, [2, [3, [4]], [5, [6, [7], [8]]]], [9]])) (4, 5) """ d1 = d2 = -1 md = 0 for child in root.children: depth, diameter = dd(child) if diameter > md: md = diameter if depth >= d1: d2 = d1 d1 = depth elif depth > d2: d2 = depth return d1 + 1, max(md, d1 + d2 + 2) if __name__ == "__main__": import doctest doctest.testmod() </source> Back to [[Chapter 7]] 20c423b339cb5bc64f1a0828b6570939d8483b44 7.25 0 168 361 2020-09-21T01:09:22Z Algowikiadmin 1 Created page with "Use the BFS starting from the vertex v. For every node keep track of the level from the vertex v. When w is encountered for the first time the level of w is the length of the..." wikitext text/x-wiki Use the BFS starting from the vertex v. For every node keep track of the level from the vertex v. When w is encountered for the first time the level of w is the length of the shortest path. Count how many times you discover w on that level. Stop expanding nodes when you go past the length of the shortest path. Back to [[Chapter 7]] b3548ec84e84022bb76fca2840f802b1c4078669 7.27 0 169 362 2020-09-21T01:11:10Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.29 0 170 363 2020-09-21T01:11:29Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.31 0 171 364 2020-09-21T01:11:38Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.33 0 172 365 2020-09-21T01:11:44Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.35 0 173 366 2020-09-21T01:11:58Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.37 0 174 367 2020-09-21T01:12:33Z Algowikiadmin 1 Created page with "Proof by induction. A tournament with 2 vertices (1,2) has a Hamiltonian path. 1 -> 2 or vice versa Now suppose our tournament with n vertices has a Hamiltonian path 1,..,n...." wikitext text/x-wiki Proof by induction. A tournament with 2 vertices (1,2) has a Hamiltonian path. 1 -> 2 or vice versa Now suppose our tournament with n vertices has a Hamiltonian path 1,..,n. Now add a vertex (n+1) that is connected to every other node. 3 cases may occur: case1) If the first node of the n Hamiltonian path can be reached by vertex (n+1), add (n+1) to the beginning of the path. New Hamiltonian path: n+1,1,...,n case2) If the last node of the n Hamiltonian path can reach the vertex (n+1), add (n+1) to the end of the path. New Hamiltonian path: 1,...,n,n+1 case3) Take the first node of the n Hamiltonian path that can be reached by (n+1). This must exist, because we are not in case 2. Call this node k. By definition of this node, (k-1) can reach (n+1). Form the new Hamiltonian path as: 1,...,k-1,n+1,k,...,n. ---- <pre> II. Algorithm O(n^2) H = [] # Hamiltonian path Create a queue Q from all the vertices of the tournament, the order of the vertices in the queue does not matter Cycle until the queue is empty: - Extract vertex V0 from the queue - If H is empty, add V0 to H, continue - Find the vertex Vi in H: ∃ V0->Vi, where i is the minimum index # O(n) - If Vi was not found V0->Vi does not exist, then insert V0 at the end of H # O(1). All edges of V0 incoming - Otherwise, if Vi was found V0->Vi exists, then inserting V0 in H before Vi # O(n) III. Algorithm O(n*logn) 1. Initializing the adjacency matrix A # The time complexity of initialization is O (n^2) in any case, since the graph is complete => processing n*(n-1)/2 edges - The matrix is initialized with n arrays of n elements. After adding arrays, assign 0 to all elements on the main diagonal # Graph without loops 2. Data preparation - Mentally change the complete undirected graph with the complete strongly connected directed graph - Eliminating strong connectivity - for each pair (V, W), remove 1 of the edges A[V][W] = 0 or A[W][V] = 0 3. Finding the Hamiltonian path - If A[s_i][s_j] == 1, there is an edge s_i->s_j => s_i dominates s_j => s_i > s_j - As it is possible to establish a relationship between the vertices, it is possible to sort them out # O(n*logn) </pre> <source lang="python"> """ O(n*logn) implementation """ from random import randint from functools import cmp_to_key from time import perf_counter for m in range(100): cap = 12 size = 2 ** cap graph = [[1 for i in range(size)] for k in range(size)] # Adjacency matrix representation for i in range(size): # Building 1 big tournament for j in range(i, size): if randint(0, 1) or j == i: # Setting directions and removing loops graph[i][j] = 0 # Removing i -> j else: graph[j][i] = 0 # Removing j -> i for i in range(size): # Tournament correctness check for j in range(size): if i != j: assert graph[i][j] != graph[j][i] else: assert graph[i][j] == 0 for z in range(2, cap + 1): # Finding Hamiltonian paths for subgraphs subgraph_size = 2 ** z start_time = perf_counter() hamiltonian_path = sorted(range(subgraph_size), key=cmp_to_key(lambda a, b: graph[b][a] - graph[a][b])) # print(hamiltonian_path, end='\n\n') # Output path print('Time: {:f}s | Size: {:d}'.format(perf_counter() - start_time, subgraph_size)) for i in range(subgraph_size - 1): # Hamiltonian path check assert graph[hamiltonian_path[i]][hamiltonian_path[i + 1]] # Next vertex in path must be reachable print('') # New line between cases </source>--[[User:Bkarpov96|Bkarpov96]] ([[User talk:Bkarpov96|talk]]) 17:34, 8 July 2020 (UTC) Back to [[Chapter 7]] 6a92fbd46ef1cdd19136be41396c0093a5fdb588 7.39 0 175 368 2020-09-21T01:13:08Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.41 0 176 369 2020-09-21T01:13:23Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 7.43 0 177 370 2020-09-21T01:13:37Z Algowikiadmin 1 Created page with " Back to [[Chapter 7]]" wikitext text/x-wiki Back to [[Chapter 7]] f108346d117a5f2f29c108402a8dbc58b05a5fdd 10.3 0 178 371 2020-09-21T01:15:35Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.5 0 179 372 2020-09-21T01:15:40Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.1 0 180 373 2020-09-21T01:15:49Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.7 0 181 374 2020-09-21T01:16:08Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.9 0 182 375 2020-09-21T01:16:21Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.13 0 183 376 2020-09-21T01:17:14Z Algowikiadmin 1 Created page with "==== 1 ==== # 20 x 1 # 1 x 6 + 14 x 1 # 2 x 6 + 8 x 1 # 3 x 6 + 2 x 1 # 1 x 10 + 10 x 1 # 1 x 10 + 1 x 6 + 4 x 1 # 2 x 10 ==== 2 ==== More generally: # there is always o..." wikitext text/x-wiki ==== 1 ==== # 20 x 1 # 1 x 6 + 14 x 1 # 2 x 6 + 8 x 1 # 3 x 6 + 2 x 1 # 1 x 10 + 10 x 1 # 1 x 10 + 1 x 6 + 4 x 1 # 2 x 10 ==== 2 ==== More generally: # there is always one way of making zero from any set of denominations; # there is one way of making any non-zero sum from one denomination provided that the sum is evenly divisible by the denomination; # for larger sets of denominations, the number of ways to make a sum S from a set D = { a, b, ... m, n } where Di < Dj for i < j, is the sum of the ways to make S, S - n, ... S - xn from D' = { a, b, c, ... m }. Implementation in C: #include <assert.h> #include <stdio.h> #include <stdlib.h> static unsigned make_change (unsigned sum, unsigned *coins, size_t nr_coins) { unsigned **table = malloc (nr_coins * sizeof *table); assert (table != NULL); for (size_t c = 0; c < nr_coins; ++c) { table[c] = malloc ((1 + sum) * sizeof *table[c]); assert (table[c] != NULL); if (c == 0) { /* * No. of ways of making s from coins[0]: 1 if s is evenly divisible by * coins[0], 0 otherwise. */ for (unsigned s = 0; s <= sum; ++s) table[0][s] = s % coins[0] == 0; } else { /* * One way of making 0 from { coins[0] .. coins[c] }: the empty set. */ table[c][0] = 1; /* * No. of ways of making s from { coins[0] .. coins[c] }: no. of ways of * making s - n * coins[c] for n > 0 and s >= n * coins[c] * from { coins[0] .. coins[c - 1] } */ for (unsigned s = 1; s <= sum; ++s) for (unsigned n = 0; n * coins[c] <= s; n ++) table[c][s] += table[c - 1][s - (n * coins[c])]; } } unsigned rv = table[nr_coins - 1][sum]; for (size_t c = 0; c < nr_coins; ++c) free (table[c]); free (table); return rv; } static int cmp_coins (const void *a, const void *b) { return (*(unsigned *) a > *(unsigned *) b) - (*(unsigned *) a < *(unsigned *) b); } int main (int argc, char **argv) { if (argc < 3) exit (EXIT_FAILURE); unsigned sum = atoi (argv[1]); size_t nr_coins = argc - 2; unsigned *coins = malloc (nr_coins * sizeof *coins); assert (coins != NULL); for (int i = 2; i < argc; ++i) coins[i - 2] = atoi (argv[i]); qsort (coins, nr_coins, sizeof *coins, cmp_coins); fprintf (stderr, "%u\n", make_change (sum, coins, nr_coins)); free (coins); exit (EXIT_SUCCESS); } Back to [[Chapter 10]] b7e917bf643812f6230032ec9262ab9e8a02b5d9 10.11 0 184 377 2020-09-21T01:17:56Z Algowikiadmin 1 Created page with "Answer to both a) and b) is no. Knapsack problem is NP-complete. ---- (a) Yes, this is a special case of the Knapsack problem where the value of each item is the same (desc..." wikitext text/x-wiki Answer to both a) and b) is no. Knapsack problem is NP-complete. ---- (a) Yes, this is a special case of the Knapsack problem where the value of each item is the same (described in section 13.10 of the book). If we have n programmes with sizes s1 to sn, where si <= sj if i < j, and we can fit the first k on the disk, there can be no larger subset, since in order to fit the (k+1)th item we must remove at least one other item of smaller or equal size. (a) No. Let D = 10, and P = { 5, 4, 2, 1, 1, 1 }. Back to [[Chapter 10]] 3874feb0618c1ffa56cbadf13d089b2f77a03989 Chapter 10 0 13 378 228 2020-09-21T01:19:01Z Algowikiadmin 1 /* Number Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center><math> \begin{array}{|C|111111111} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 & 17 & 20 \\ \end{array}</math> </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] f4c9ab9924b4e041a7c8aa92c38755847a8b5328 379 378 2020-09-21T01:29:02Z Algowikiadmin 1 /* Number Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center><math> \begin{array}{|c|c|c|c|c|c|c|c||c|} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\ \hline price&1&5&8&9&10&17&17&20\\ \end{array}</math> </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.125|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] c6d5c45c608fffb81d842823d0f1dfbb00ae4028 388 379 2020-09-21T13:42:15Z Algowikiadmin 1 /* Number Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center><math> \begin{array}{|c|c|c|c|c|c|c|c||c|} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\ \hline price&1&5&8&9&10&17&17&20\\ \end{array}</math> </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.25|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] 57d25ca1a0fcd7cb97f2e8692126027fededf41c 394 388 2020-09-21T13:48:15Z Algowikiadmin 1 /* Design Problems */ wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center><math> \begin{array}{|c|c|c|c|c|c|c|c||c|} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\ \hline price&1&5&8&9&10&17&17&20\\ \end{array}</math> </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.25|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]]. Consider the problem of examining a string <math>x = x_1 x_2 \ldots x_n</math> from an alphabet of <math>k</math> symbols, and a multiplication table over this alphabet. Decide whether or not it is possible to parenthesize <math>x</math> in such a way that the value of the resulting expression is <math>a</math>, where <math>a</math> belongs to the alphabet. The multiplication table is neither commutative or associative, so the order of multiplication matters. <center> <math>\begin{array}{c|ccc} & a & b & c \\ \hline a & a & c & c \\ b & a & a & b \\ c & c & c & c \\ \end{array}</math> </center> :For example, consider the above multiplication table and the string <math>bbbba</math>. Parenthesizing it <math>(b(bb))(ba)</math> gives <math>a</math>, but <math>((((bb)b)b)a)</math> gives <math>c</math>. :Give an algorithm, with time polynomial in <math>n</math> and <math>k</math>, to decide whether such a parenthesization exists for a given string, multiplication table, and goal element. [[10.37|Solution]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] 4f80f74e096f653db069e918e7bd78701dd82865 10.15 0 185 380 2020-09-21T01:31:11Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.17 0 186 381 2020-09-21T13:39:36Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.19 0 187 382 2020-09-21T13:39:44Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.21 0 188 383 2020-09-21T13:40:05Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.23 0 189 384 2020-09-21T13:40:18Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.25 0 190 385 2020-09-21T13:40:34Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 386 385 2020-09-21T13:41:40Z Algowikiadmin 1 Algowikiadmin moved page [[10.125]] to [[10.25]] wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.27 0 192 389 2020-09-21T13:44:04Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.29 0 193 390 2020-09-21T13:44:07Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.31 0 194 391 2020-09-21T13:44:41Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.33 0 195 392 2020-09-21T13:44:53Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.35 0 196 393 2020-09-21T13:44:58Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.37 0 197 395 2020-09-21T13:48:43Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.41 0 198 396 2020-09-21T13:49:16Z Algowikiadmin 1 Created page with " Back to [[Chapter 10]]" wikitext text/x-wiki Back to [[Chapter 10]] b4ecf0797d23ffe72a9dbbb48c38b5703d5b1235 10.39 0 199 397 2020-09-21T13:49:28Z Algowikiadmin 1 Created page with "== A Python Solution - O(1) == <PRE> import sys n = int(sys.argv[1]) OUT_TMP = "Min # of coins for covering %d: %d, coins used: %s" COINS = tuple(sorted((3, 4, 9, 20, 22, 23)..." wikitext text/x-wiki == A Python Solution - O(1) == <PRE> import sys n = int(sys.argv[1]) OUT_TMP = "Min # of coins for covering %d: %d, coins used: %s" COINS = tuple(sorted((3, 4, 9, 20, 22, 23), reverse=True)) FAILSAFE_MAX = 9999999999 if len(COINS) < 1: raise Exception("Coins please!") if len(COINS) == 1: c = COINS[0] if n % c == 0: print OUT_TMP % (n, n/c, [c]*(n/c)) sys.exit() else: raise Exception("Impossible!") CAL_MAX = min(COINS[0] * COINS[1], n+1) min_coins_n = [0] min_coins_l = [()] for i in xrange(1, CAL_MAX): if i in COINS: min_coins_n.append(1) min_coins_l.append((i,)) else: current_min = -1 for c in COINS: smaller_i = i - c if smaller_i > 0 and min_coins_n[smaller_i] < FAILSAFE_MAX and \ (current_min == -1 or min_coins_n[smaller_i] < min_coins_n[current_min]): current_min = smaller_i if current_min != -1: min_coins_n.append(min_coins_n[current_min] + 1) l = list(min_coins_l[current_min]) # copy l.append(i - current_min) # another copy, not required but robuster code ll = tuple(l) min_coins_l.append(tuple(l)) else: # not changable using the current coins min_coins_n.append(FAILSAFE_MAX) min_coins_l.append(None) big_part_n = 0 coins_list = [] remaining_n = n if n > CAL_MAX: n_big_part = n - CAL_MAX big_part_n = (n_big_part / COINS[0]) + 1 coins_list = [COINS[0]] * big_part_n remaining_n -= big_part_n * COINS[0] final_n = big_part_n + min_coins_n[remaining_n] if final_n >= FAILSAFE_MAX: raise Exception("Impossible!") coins_list.extend(min_coins_l[remaining_n]) print OUT_TMP % (n, final_n, coins_list) </PRE> == A Java solution - O(n) == <PRE> public class CoinProblem { public static void main(String[] args) { int sumToMakeChangeFor = 20; int[] coinDenominations = new int[]{1, 2, 5, 10}; System.out.println("Number of coins needed = " + numberOfCoinsNeededForChange(sumToMakeChangeFor, coinDenominations)); } private static int numberOfCoinsNeededForChange(int sum, int[] coinDenominations) { int[] minimumNumberOfCoinsNeeded = new int[sum + 1]; for (int i = 0; i <= sum; i++){ minimumNumberOfCoinsNeeded[i] = Integer.MAX_VALUE; } minimumNumberOfCoinsNeeded[0] = 0; for (int currentSum = 1; currentSum <= sum; currentSum++) { for (int j = 0; j < coinDenominations.length; j++) { if (coinDenominations[j] <= currentSum && ((minimumNumberOfCoinsNeeded[currentSum - coinDenominations[j]] + 1) < minimumNumberOfCoinsNeeded[currentSum])) minimumNumberOfCoinsNeeded[currentSum] = minimumNumberOfCoinsNeeded[currentSum - coinDenominations[j]] + 1; } } return minimumNumberOfCoinsNeeded[sum]; } } </PRE> Reference: [http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg Dynamic Programming, TopCoder.com] Back to [[Chapter 10]] a1616e35bb8b32a6b816015f9a67d217f3473f61 9.1 0 200 398 2020-09-21T13:51:36Z Algowikiadmin 1 Created page with "== Algorithm == Given <code>a</code>, the input array, and <code>curr</code>, the derangement built up so far: # If <code>curr</code> represents a complete solution, print i..." wikitext text/x-wiki == Algorithm == Given <code>a</code>, the input array, and <code>curr</code>, the derangement built up so far: # If <code>curr</code> represents a complete solution, print it. # Otherwise, examine all the possibilities for the next element of the derangement. The next element must come from the input array <code>a</code>. It must not already have been used so far (<code>!curr.contains(a[i]</code>), and it must not be the element at the same position in the input array (<code>i != curr.size()</code>). ## For each remaining possibility, add it to the current derangement and recurse. == Implementation in Java == <pre> public class DerangementGenerator { public void derangements(int[] a) { d(a, new LinkedList<Integer>()); } public void d(int[] a, LinkedList<Integer> curr) { if (curr.size() == a.length) print(curr); else { for (int i = 0; i < a.length; i++) { if (!curr.contains(a[i]) && i != curr.size()) { curr.addLast(a[i]); // O(1) d(a, curr); curr.removeLast(); // O(1) } } } } public void print(List<Integer> l) { for (int i = 0; i < l.size() - 1; i++) { System.out.print(l.get(i) + ", "); } System.out.println(l.get(l.size() - 1)); } public static void main(String[] args) { if (args.length < 1) { System.err.println("Usage: java DerangementGenerator N"); System.exit(-1); } int n = Integer.parseInt(args[0]); DerangementGenerator dg = new DerangementGenerator(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } dg.derangements(a); } } </pre> Back to [[Chapter 9]] e497f37373911ab40a1906e0339762ce645c1816 9.3 0 201 399 2020-09-21T13:51:55Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.5 0 202 400 2020-09-21T13:52:01Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.7 0 203 401 2020-09-21T13:52:29Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.9 0 204 402 2020-09-21T13:52:42Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.11 0 205 403 2020-09-21T14:00:01Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.13 0 206 404 2020-09-21T14:00:10Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.15 0 207 405 2020-09-21T14:00:22Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.17 0 208 406 2020-09-21T14:00:40Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.19 0 209 407 2020-09-21T14:00:44Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e Chapter 9 0 12 408 230 2020-09-21T14:00:59Z Algowikiadmin 1 /* Combinatorial Optimization */ wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math> permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary. :[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once. [[9.17|Solution]] :9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board: <center><math>\begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix}</math></center> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>. :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are: <center><math>\begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} </math></center> [[9.19|Solution]] :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph. ===Combinatorial Optimization=== For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice? :[[9.21]]. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470). [[9.21|Solution]] :9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537). :[[9.23]]. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586). [[9.23|Solution]] :9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604). :[[9.25]]. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608). [[9.25|Solution]] :9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618). :[[9.27]]. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678). [[9.27|Solution]] ===Interview Problems=== :9.28. Write a function to find all permutations of the letters in a given string. :[[9.29]]. Implement an efficient algorithm for listing all <math>k</math>-element subsets of <math>n</math> items. [[9.29|Solution]] :9.30. An anagram is a rearrangement of the letters in a given string into a sequence of dictionary words, like ''Steven Skiena'' into ''Vainest Knees''. Propose an algorithm to construct all the anagrams of a given string. :[[9.31]]. Telephone keypads have letters on each numerical key. Write a program that generates all possible words resulting from translating a given digit sequence (e.g. 145345) into letters. [[9.31|Solution]] :9.32. You start with an empty room and a group of <math>n</math> people waiting outside. At each step, you may either admit one person into the room, or let one out. Can you arrange a sequence of <math>2n</math> steps, so that every possible combination of people is achieved exactly once? :[[9.33]]. Use a random number generator (rng04) that generates numbers from {0, 1, 2, 3, 4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability. What is the expected number of calls to rng04 per call of rng07? [[9.33|Solution]] Back to [[Chapter List]] 4bcea818ad0982cfdad52cbf0797c83ee0fbf052 9.21 0 210 409 2020-09-21T14:01:43Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.23 0 211 410 2020-09-21T14:01:48Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.25 0 212 411 2020-09-21T14:01:51Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.27 0 213 412 2020-09-21T14:01:55Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.29 0 214 413 2020-09-21T14:02:31Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.31 0 215 414 2020-09-21T14:02:38Z Algowikiadmin 1 Created page with " Back to [[Chapter 9]]" wikitext text/x-wiki Back to [[Chapter 9]] 1363f1683f0f6c292946fca4fa86a1eb2d77f79e 9.33 0 216 415 2020-09-21T14:02:54Z Algowikiadmin 1 Created page with "We avoid sums and products and throw away parts of the sample space we don't need. At the same time, we want to be efficient in our use of the space. Conceptually we create a..." wikitext text/x-wiki We avoid sums and products and throw away parts of the sample space we don't need. At the same time, we want to be efficient in our use of the space. Conceptually we create a simple tree branching four ways on the first draw from <code>rng04</code> (throwing away one value) and then drawing two possible values (say low or high) from the second draw (throwing away one value). Each end point is equally likely and draws are independent so this would provide a uniform sample. <pre> #!/usr/bin/env python import sys, random def rng04(): return random.randint(0,4) # filtered call to rng04 # return 0 1 2 3 def rng03(): while True: i = rng04() if i < 4: return i # filtered call to rng04 # return 0 1 # use rng04 instead of rng03 to make analysis easier def rng01(): while True: i = rng04() if i < 4: return i % 2 # generate uniform numbers in 0 1 2 3 4 5 6 7 def rng07(): i = rng03() j = rng01() return i * 2 + j if __name__ == "__main__": for i in range(10000): print rng07() sys.exit() </pre> Notice we're not <i>summing</i> we are indexing uniformly on the filtered values. Summing can skew the distribution away from uniform when it generates more than one way to create a specific value or a differing number than for all other values (e.g. rolling two six sided dice, etc.) Second part of question asks for <i>expected</i> of calls to <code>rng04</code>. The expected number of calls to <code>rng04</code> if we are filtering calls to <code>rng04</code> is <math>1/p</math> where <math>p</math> is the probability of accepting a value. For both of the filtered calls we are throwing away one of the five values so the <math>p = 0.8</math> and the expected number of calls is <math>1.25</math>. We are making two filtered calls to <code>rng04</code> in <code>rng07</code> -- since the <i>expected</i> value of a sum is just the sum of the expected values we know that each call to <code>rng07</code> has an the expected number of calls to <code>rng04</code> of <math>2.5</math>. Back to [[Chapter 9]] 20cc3e00d779c106fed682bb1789393aca6b1a0c 8.1 0 217 416 2020-09-21T14:04:53Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.3 0 218 417 2020-09-21T14:06:03Z Algowikiadmin 1 Created page with "No. Counter example provided below: G(V,E,W) = ((A,B,C,D),({A,B},{B,C},{C,D},{D,A}),(1,2,3,4)) Minimum spanning tree has a weight of 6 with edges {A,B},{B,C},{C,D}. In the..." wikitext text/x-wiki No. Counter example provided below: G(V,E,W) = ((A,B,C,D),({A,B},{B,C},{C,D},{D,A}),(1,2,3,4)) Minimum spanning tree has a weight of 6 with edges {A,B},{B,C},{C,D}. In the full graph the minimum distance between A and D is 4. Back to [[Chapter 8]] eb686b97081afa3e17edd883eb1c3ee099266647 8.5 0 219 418 2020-09-21T14:06:45Z Algowikiadmin 1 Created page with "In both algorithms, an edge can only ever be picked once, so they will both eventually terminate regardless of negative edge weights. I also suspect that they still generate..." wikitext text/x-wiki In both algorithms, an edge can only ever be picked once, so they will both eventually terminate regardless of negative edge weights. I also suspect that they still generate minimum spanning trees, but don't have a proof of it. Back to [[Chapter 8]] 31498a2bee84d6d59fb2856b1831c0352159f966 8.7 0 220 419 2020-09-21T14:07:10Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.9 0 221 420 2020-09-21T14:07:25Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.11 0 222 421 2020-09-21T14:07:33Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.13 0 223 422 2020-09-21T14:07:54Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 Chapter 8 0 11 423 238 2020-09-21T14:08:42Z Algowikiadmin 1 /* Shortest Paths */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem. [[8.15|Solution]] :8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>? :[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>. :(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>. :(c)Can the two trees be completely disjointed? [[8.17|Solution]] :8.18. Either prove the following or give a counterexample: :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>. [[8.19|Solution]] :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path. :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph. [[8.21|Solution]] :8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm? :[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time. [[8.23|Solution]] :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample. :[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>. [[8.25|Solution]] :8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple. :[[8.27]].''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path. [[8.27|Solution]] ===Network Flow and Matching=== :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree. :[[8.29]]. An edge cover of an undirected graph <math>G = (V, E)</math> is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for <math>G</math>. [[8.29|Solution]] Back to [[Chapter List]] c6bd5251135c26622d24d9e234b170b993ace530 428 423 2020-09-21T14:10:28Z Algowikiadmin 1 /* Shortest Paths */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem. [[8.15|Solution]] :8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>? :[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>. :(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>. :(c)Can the two trees be completely disjointed? [[8.17|Solution]] :8.18. Either prove the following or give a counterexample: :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>. [[8.19|Solution]] :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path. :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph. [[8.21|Solution]] :8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm? :[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time. [[8.23|Solution]] :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample. :[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>. [[8.25|Solution]] :8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple. :[[8.27]]. ''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path. [[8.27|Solution]] ===Network Flow and Matching=== :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree. :[[8.29]]. An edge cover of an undirected graph <math>G = (V, E)</math> is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for <math>G</math>. [[8.29|Solution]] Back to [[Chapter List]] 0d59208db14fc6e002b0203d7af137661b163e71 433 428 2020-09-21T14:13:01Z Algowikiadmin 1 /* Shortest Paths */ wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8.15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem. [[8.15|Solution]] :8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>? :[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>. :(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>. :(c)Can the two trees be completely disjointed? [[8.17|Solution]] :8.18. Either prove the following or give a counterexample: :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>. [[8.19|Solution]] :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path. :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph. [[8.21|Solution]] :8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm? :[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time. [[8.23|Solution]] :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample. :[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>. [[8.25|Solution]] :8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple. :[[8.27]]. ''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path. [[8.27|Solution]] ===Network Flow and Matching=== :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree. :[[8.29]]. An edge cover of an undirected graph <math>G = (V, E)</math> is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for <math>G</math>. [[8.29|Solution]] Back to [[Chapter List]] 77e827584b838786246004c28b13ef07dd2ec18c 8.15 0 224 424 2020-09-21T14:09:09Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.17 0 225 425 2020-09-21T14:09:33Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.19 0 226 426 2020-09-21T14:09:39Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.21 0 227 427 2020-09-21T14:09:46Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.23 0 228 429 2020-09-21T14:10:45Z Algowikiadmin 1 Created page with " Back to [[Chapter 8]]" wikitext text/x-wiki Back to [[Chapter 8]] 0d5556dc3ac087faa70bc285ec16bbd5f1edeaa7 8.25 0 229 430 2020-09-21T14:11:39Z Algowikiadmin 1 Created page with "Step 1: Perform topological sorting of the graph (we can do it as Graph is acyclic). This is O(n + m) Step 2: Go through vertices in topological order. Initially all vertic..." wikitext text/x-wiki Step 1: Perform topological sorting of the graph (we can do it as Graph is acyclic). This is O(n + m) Step 2: Go through vertices in topological order. Initially all vertices marked as inaccessible and only starting vertex marked with 0 distance. For every vertex in cycle, if it is accessible, we update all its children with new distance if new distance is shorter then previous one. Topological sorting ensures that we don't ever need to go backtrack. This is O(n + m). Total running bound is O(n+m), which is linear as requested. Back to [[Chapter 8]] 3c676a834e25e63e10417ce25f994949608b5c55 8.27 0 230 431 2020-09-21T14:12:11Z Algowikiadmin 1 Created page with "The problem reduces to Floyd - Warshall algorithm if you take logs of all currency-exchange rates, as if a * b = c then ln(a) + ln(b) = ln(c). In computational finance people..." wikitext text/x-wiki The problem reduces to Floyd - Warshall algorithm if you take logs of all currency-exchange rates, as if a * b = c then ln(a) + ln(b) = ln(c). In computational finance people often use logarithms of prices instead of prices themselves, as it gives faster calculations and makes it easy to apply log-normal distribution instead of normal distribution (log-normal distribution is more appropriate for asset prices). Back to [[Chapter 8]] b17c459bb187347738fc51d57739e854b5faa8d6 8.29 0 231 432 2020-09-21T14:12:28Z Algowikiadmin 1 Created page with "1. Find maximum matching. Bipartite matching is described in the book. General matching would require Edmonds Blossom algorithm. 2. Include an arbitrary edge for every uncover..." wikitext text/x-wiki 1. Find maximum matching. Bipartite matching is described in the book. General matching would require Edmonds Blossom algorithm. 2. Include an arbitrary edge for every uncovered vertex. A greedy algorithm suffices. Back to [[Chapter 8]] 7f2551f51edcace8aec48261b01fdff25ce5c48a Main Page 0 1 434 11 2020-09-28T17:26:46Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with Steven Skiena's The Algorithm Design Manual. Students and other readers are encouraged to contribute hints and answers to all odd numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. [[Chapter List]] == Getting started == * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ] * [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language] * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Combating_spam Learn how to combat spam on your wiki] f9a884d4a705756d8bd43dabe2c4298d3114b2df 435 434 2020-09-28T17:27:37Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with Steven Skiena's The Algorithm Design Manual. Students and other readers are encouraged to contribute hints and answers to all odd numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. [[Chapter List]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) b125a29657a1dd5a040ecf9509e7424027f50073 436 435 2020-09-28T17:29:58Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. [[Chapter List]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) e6fd87c682953493ed5f342b136012689c48e80b 440 436 2020-09-28T17:54:50Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. The chapters and their corresponding exercises can be found [[Chapter List|here]]. == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 489fd928270394cc1fa191d37e4cc2f453f4e2fd 441 440 2020-09-28T17:55:00Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. The chapters and their corresponding exercises can be found [[Chapter List|here]]. == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) d2dbf55f192312410d07728ffa9d3bc18512cee6 442 441 2020-10-01T18:00:51Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. ==Chapters== #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 51b2f9bd9e048895d31ce8614fd8424cdc4857c9 443 442 2020-10-01T18:01:33Z Algowikiadmin 1 /* Chapters */ wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) ba9f446af835d3e6a5db6923bdbce743d464c413 444 443 2020-10-01T18:01:44Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 2845102c46a6229c9c7962166b81fd8d454409d8 445 444 2020-10-01T18:05:31Z Algowikiadmin 1 Protected "[[Main Page]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 2845102c46a6229c9c7962166b81fd8d454409d8 User:Algowikiadmin 2 232 437 2020-09-28T17:32:43Z Algowikiadmin 1 Created page with "This admin account belongs to those that created and moderate this Wiki." wikitext text/x-wiki This admin account belongs to those that created and moderate this Wiki. d319248e1460ec12534664611c52d3e3a52ea2c8 Chapter 3 0 6 438 297 2020-09-28T17:37:22Z Algowikiadmin 1 /* Data Structure */ wikitext text/x-wiki =Data Structures= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>1, 2, .., n</math>, and initialization can take <math>O(n)</math> time. [[3.11|Solution]] :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an <math>O(n)</math> algorithm to find the maximum depth of a binary tree with <math>n</math> nodes. :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an <math>O(n)</math> algorithm to identify these two elements so they can be swapped back. [[3.13|Solution]] :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order. :[[3.15]]. Describe an <math>O(n)</math>-time algorithm that takes an <math>n</math>-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1. [[3.15|Solution]] :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes: :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes. :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes. :[[3.17]]. Give an <math>O(n)</math> algorithm that determines whether a given <math>n</math>-node binary tree is height-balanced (see Problem 3-15). [[3.17|Solution]] :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this? :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O(log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) [[3.19|Solution]] :3.20. Design a data structure to support the following operations: :• ''insert''(<math>x,T</math>) – Insert item <math>x</math> into the set <math>T</math>. :• ''delete''(<math>k,T</math>) – Delete the <math>k</math>th smallest element from <math>T</math>. :• ''member''(<math>x,T</math>) – Return true iff <math>x \in T</math>. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.21]]. A ''concatenate operation'' takes two sets <math>S_1</math> and <math>S_2</math>, where every key in <math>S_1</math> is smaller than any key in <math>S_2</math>, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal height of the two trees. [[3.21|Solution]] ===Applications of Tree Structures=== :3.22. Design a data structure that supports the following two operations: :• ''insert''(<math>x</math>) – Insert item <math>x</math> from the data stream to the data structure. :• ''median''() – Return the median of all elements so far. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of <math>n</math> strings, each of length at most <math>l</math>. We seek to print out all strings beginning with a particular prefix <math>p</math>. Show how to do this in <math>O(ml log n)</math> time, where <math>m</math> is the number of strings. [[3.23|Solution]] :3.24. An array <math>A</math> is called <math>k</math>-unique if it does not contain a pair of duplicate elements within <math>k</math> positions of each other, that is, there is no <math>i</math> and <math>j</math> such that <math>A[i] = A[j]</math> and <math>|j - i| \leq k</math>. Design a worst-case <math>O(n log k)</math> algorithm to test if <math>A</math> is <math>k</math>-unique. :[[3.25]]. In the ''bin-packing problem'', we are given <math>n</math> objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, with each bin holding 1 kilogram at most. :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the <math>n</math> weights <math>w_1, w_2, ..., w_n</math> and outputting the number of bins used) in <math>O(n log n)</math> time. :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted. [[3.25|Solution]] :3.26. Suppose that we are given a sequence of <math>n</math> values <math>x_1, x_2, ..., x_n</math> and seek to quickly answer repeated queries of the form: given <math>i</math> and <math>j</math>, find the smallest value in <math>x_i, . . . , x_j</math>. :(a) Design a data structure that uses <math>O(n^2)</math> space and answers queries in <math>O(1)</math> time. :(b) Design a data structure that uses <math>O(n)</math> space and answers queries in <math>O(log n)</math> time. For partial credit, your data structure can use <math>O(n log n)</math> space and have <math>O(log n)</math> query time. :[[3.27]]. Suppose you are given an input set <math>S</math> of <math>n</math> integers, and a black box that if given any sequence of integers and an integer <math>k</math> instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly <math>k</math>. Show how to use the black box <math>O(n)</math> times to find a subset of <math>S</math> that adds up to <math>k</math>. [[3.27|Solution]] :3.28. Let <math>A[1..n]</math> be an array of real numbers. Design an algorithm to perform any sequence of the following operations: :• ''Add''(<math>i,y</math>) – Add the value <math>y</math> to the <math>i</math>th number. :• ''Partial-sum''(<math>i</math>) – Return the sum of the first <math>i</math> numbers, that is, <math>\sum_{j=1}^i A[j]</math>. :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take <math>O(log n)</math> steps. You may use one additional array of size <math>n</math> as a work space. :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different. :• ''Add''(<math>k,y</math>) – Add the value <math>y</math> to the item with key <math>k</math>. :• ''Insert''(<math>k,y</math>) – Insert a new item with key <math>k</math> and value <math>y</math>. :• ''Delete''(<math>k</math>) – Delete the item with key <math>k</math>. :• ''Partial-sum''(<math>k</math>) – Return the sum of all the elements currently in the set whose key is less than <math>k</math>, that is, <math>\sum_{i < k} x_i</math>. :The worst-case running time should still be <math>O(n log n)</math> for any sequence of <math>O(n)</math> operations. [[3.29|Solution]] :3.30. You are consulting for a hotel that has <math>n</math> one-bed rooms. When a guest checks in, they ask for a room whose number is in the range <math>[l, h]</math>. Propose a data structure that supports the following data operations in the allotted time: :(a) ''Initialize''(<math>n</math>): Initialize the data structure for empty rooms numbered <math>1, 2, . . . , n</math>, in polynomial time. :(b) ''Count''(<math>l, h</math>): Return the number of available rooms in <math>[l, h]</math>, in <math>O(log n)</math> time. :(c) ''Checkin''(<math>l, h</math>): In <math>O(log n)</math> time, return the first empty room in <math>[l, h]</math> and mark it occupied, or return NIL if all the rooms in <math>[l, h]</math> are occupied. :(d) ''Checkout''(<math>x</math>): Mark room <math>x</math> as unoccupied, in <math>O(log n)</math> time. :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer <math>X</math> in <math>O(1)</math> time (i.e., constant time, independent of the total number of integers stored). Assume that <math>1 \leq X \leq n</math> and that there are <math>m + n</math> units of space available, where <math>m</math> is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays <math>A[1..n]</math> and <math>B[1..m]</math>.) You are not allowed to initialize either <math>A</math> or <math>B</math>, as that would take <math>O(m)</math> or <math>O(n)</math> operations. This means the arrays are full of random garbage to begin with, so you must be very careful. [[3.31|Solution]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] b0817fce8f5c556b0a0bdd29b86a55a959b64c58 448 438 2020-10-01T18:06:35Z Algowikiadmin 1 Protected "[[Chapter 3]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Data Structures= ===Stacks, Queues, and Lists=== :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. [[3.1|Solution]] :3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.) :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. [[3.3|Solution]] :3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time. :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost. :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion. [[3.5|Solution]] :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it? :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. [[3.7|Solution]] ===Elementary Data Structures=== :3.8. Tic-tac-toe is a game played on an <math>n * n</math> board (typically <math>n = 3</math>) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with <math>O(n)</math> space that accepts a sequence of moves, and reports in constant time whether the last move won the game. :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of <math>n</math> words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words. [[3.9|Solution]] :3.10. Two strings <math>X</math> and <math>Y</math> are anagrams if the letters of <math>X</math> can be rearranged to form <math>Y</math> . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings <math>X</math> and <math>Y</math> are anagrams. ===Trees and Other Dictionary Structures=== :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>1, 2, .., n</math>, and initialization can take <math>O(n)</math> time. [[3.11|Solution]] :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an <math>O(n)</math> algorithm to find the maximum depth of a binary tree with <math>n</math> nodes. :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an <math>O(n)</math> algorithm to identify these two elements so they can be swapped back. [[3.13|Solution]] :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order. :[[3.15]]. Describe an <math>O(n)</math>-time algorithm that takes an <math>n</math>-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1. [[3.15|Solution]] :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes: :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes. :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes. :[[3.17]]. Give an <math>O(n)</math> algorithm that determines whether a given <math>n</math>-node binary tree is height-balanced (see Problem 3-15). [[3.17|Solution]] :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this? :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O(log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) [[3.19|Solution]] :3.20. Design a data structure to support the following operations: :• ''insert''(<math>x,T</math>) – Insert item <math>x</math> into the set <math>T</math>. :• ''delete''(<math>k,T</math>) – Delete the <math>k</math>th smallest element from <math>T</math>. :• ''member''(<math>x,T</math>) – Return true iff <math>x \in T</math>. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.21]]. A ''concatenate operation'' takes two sets <math>S_1</math> and <math>S_2</math>, where every key in <math>S_1</math> is smaller than any key in <math>S_2</math>, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal height of the two trees. [[3.21|Solution]] ===Applications of Tree Structures=== :3.22. Design a data structure that supports the following two operations: :• ''insert''(<math>x</math>) – Insert item <math>x</math> from the data stream to the data structure. :• ''median''() – Return the median of all elements so far. :All operations must take <math>O(log n)</math> time on an <math>n</math>-element set. :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of <math>n</math> strings, each of length at most <math>l</math>. We seek to print out all strings beginning with a particular prefix <math>p</math>. Show how to do this in <math>O(ml log n)</math> time, where <math>m</math> is the number of strings. [[3.23|Solution]] :3.24. An array <math>A</math> is called <math>k</math>-unique if it does not contain a pair of duplicate elements within <math>k</math> positions of each other, that is, there is no <math>i</math> and <math>j</math> such that <math>A[i] = A[j]</math> and <math>|j - i| \leq k</math>. Design a worst-case <math>O(n log k)</math> algorithm to test if <math>A</math> is <math>k</math>-unique. :[[3.25]]. In the ''bin-packing problem'', we are given <math>n</math> objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, with each bin holding 1 kilogram at most. :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the <math>n</math> weights <math>w_1, w_2, ..., w_n</math> and outputting the number of bins used) in <math>O(n log n)</math> time. :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted. [[3.25|Solution]] :3.26. Suppose that we are given a sequence of <math>n</math> values <math>x_1, x_2, ..., x_n</math> and seek to quickly answer repeated queries of the form: given <math>i</math> and <math>j</math>, find the smallest value in <math>x_i, . . . , x_j</math>. :(a) Design a data structure that uses <math>O(n^2)</math> space and answers queries in <math>O(1)</math> time. :(b) Design a data structure that uses <math>O(n)</math> space and answers queries in <math>O(log n)</math> time. For partial credit, your data structure can use <math>O(n log n)</math> space and have <math>O(log n)</math> query time. :[[3.27]]. Suppose you are given an input set <math>S</math> of <math>n</math> integers, and a black box that if given any sequence of integers and an integer <math>k</math> instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly <math>k</math>. Show how to use the black box <math>O(n)</math> times to find a subset of <math>S</math> that adds up to <math>k</math>. [[3.27|Solution]] :3.28. Let <math>A[1..n]</math> be an array of real numbers. Design an algorithm to perform any sequence of the following operations: :• ''Add''(<math>i,y</math>) – Add the value <math>y</math> to the <math>i</math>th number. :• ''Partial-sum''(<math>i</math>) – Return the sum of the first <math>i</math> numbers, that is, <math>\sum_{j=1}^i A[j]</math>. :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take <math>O(log n)</math> steps. You may use one additional array of size <math>n</math> as a work space. :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different. :• ''Add''(<math>k,y</math>) – Add the value <math>y</math> to the item with key <math>k</math>. :• ''Insert''(<math>k,y</math>) – Insert a new item with key <math>k</math> and value <math>y</math>. :• ''Delete''(<math>k</math>) – Delete the item with key <math>k</math>. :• ''Partial-sum''(<math>k</math>) – Return the sum of all the elements currently in the set whose key is less than <math>k</math>, that is, <math>\sum_{i < k} x_i</math>. :The worst-case running time should still be <math>O(n log n)</math> for any sequence of <math>O(n)</math> operations. [[3.29|Solution]] :3.30. You are consulting for a hotel that has <math>n</math> one-bed rooms. When a guest checks in, they ask for a room whose number is in the range <math>[l, h]</math>. Propose a data structure that supports the following data operations in the allotted time: :(a) ''Initialize''(<math>n</math>): Initialize the data structure for empty rooms numbered <math>1, 2, . . . , n</math>, in polynomial time. :(b) ''Count''(<math>l, h</math>): Return the number of available rooms in <math>[l, h]</math>, in <math>O(log n)</math> time. :(c) ''Checkin''(<math>l, h</math>): In <math>O(log n)</math> time, return the first empty room in <math>[l, h]</math> and mark it occupied, or return NIL if all the rooms in <math>[l, h]</math> are occupied. :(d) ''Checkout''(<math>x</math>): Mark room <math>x</math> as unoccupied, in <math>O(log n)</math> time. :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer <math>X</math> in <math>O(1)</math> time (i.e., constant time, independent of the total number of integers stored). Assume that <math>1 \leq X \leq n</math> and that there are <math>m + n</math> units of space available, where <math>m</math> is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays <math>A[1..n]</math> and <math>B[1..m]</math>.) You are not allowed to initialize either <math>A</math> or <math>B</math>, as that would take <math>O(m)</math> or <math>O(n)</math> operations. This means the arrays are full of random garbage to begin with, so you must be very careful. [[3.31|Solution]] ===Implementation Projects=== :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions. :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts. [[3.33|Solution]] ===Interview Problems=== :3.34. What method would you use to look up a word in a dictionary? :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval? [[3.35|Solution]] :3.36. Write a function to find the middle node of a singly linked list. :[[3.37]]. Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. [[3.37|Solution]] :3.38. Write a program to convert a binary search tree into a linked list. :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion. [[3.39|Solution]] :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time. :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. [[3.41|Solution]] :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space. :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. [[3.43|Solution]] :3.44. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: there are solutions faster than <math>O(n^2)</math>.) :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space. [[3.45|Solution]] Back to [[Chapter List]] b0817fce8f5c556b0a0bdd29b86a55a959b64c58 Chapter List 0 2 439 9 2020-09-28T17:39:25Z Algowikiadmin 1 wikitext text/x-wiki Chapters #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] d6ea00439618f14e2f38163c217f5366349fdf36 447 439 2020-10-01T18:06:08Z Algowikiadmin 1 Protected "[[Chapter List]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki Chapters #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] d6ea00439618f14e2f38163c217f5366349fdf36 Chapter 1 0 3 446 162 2020-10-01T18:05:51Z Algowikiadmin 1 Protected "[[Chapter 1]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number. =Introduction to Algorithms= ===Finding Counter Examples=== :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. [[1.1|Solution]] :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. [[1.3|Solution]] :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. #Put the elements of <math>S</math> in the knapsack from largest to smallest. [[1.5|Solution]] :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered. :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. [[1.7|Solution]] ===Proofs of Correctness=== :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. multiply(<math>y,z</math>) #Return the product <math>yz</math>. ''if'' <math>z=0</math> ''then'' return(0) ''else'' return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> horner(<math>A,x</math>) <math>p = A_n</math> for <math>i</math> from <math>n-1</math> to <math>0</math> <math>p = p*x+A_i</math> return <math>p</math> [[1.9|Solution]] :1.10. Prove the correctness of the following sorting algorithm. bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) for <math>i</math> from <math>n</math> to <math>1</math> for <math>j</math> from <math>1</math> to <math>i-1</math> if (<math>A[j] > A[j+1]</math>) swap the values of <math>A[j]</math> and <math>A[j+1]</math> :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: :::::<math>gcd(x, y) = gcd(y, x mod y)</math> :Prove that Euclid’s algorithm is correct. [[1.11|Solution]] ===Induction=== :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. [[1.13|Solution]] :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> [[1.15|Solution]] :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> [[1.17|Solution]] :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. [[1.19|Solution]] :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> ===Estimation=== :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? [[1.21|Solution]] :1.22. How many words are there in this textbook? :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. [[1.23|Solution]] :1.24. Estimate how many cities and towns there are in the United States. :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer. [[1.25|Solution]] :1.26. How many Starbucks or McDonald’s locations are there in your country? :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? [[1.27|Solution]] :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time? :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . ::(a) if you believe that the algorithm takes time proportional to n2, and ::(b) if you believe that the algorithm takes time roughly proportional to n log n? [[1.29|Solution]] ===Implementation Projects=== :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. [[1.31|Solution]] ===Interview Problems=== :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done. [[1.33|Solution]] :1.34. How many piano tuners are there in the entire world? :[[1.35]]. How many gas stations are there in the United States? [[1.35|Solution]] :1.36. How much does the ice in a hockey rink weigh? :[[1.37]]. How many miles of road are there in the United States? [[1.37|Solution]] :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? Back to [[Chapter List]] 5d9aa8fcf4b2825cda3af597916108cd330f2d45 Chapter 4 0 7 449 277 2020-10-01T18:06:49Z Algowikiadmin 1 Protected "[[Chapter 4]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Sorting= ===Applications of Sorting: Numbers=== :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. [[4.1|Solution]] :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. [[4.3|Solution]] :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. [[4.5|Solution]] :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. [[4.7|Solution]] :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. [[4.9|Solution]] :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. [[4.11|Solution]] ===Applications of Sorting: Intervals and Sets=== :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). [[4.13|Solution]] :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. [[4.15|Solution]] :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. ===Heaps=== :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. [[4.17|Solution]] :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. :(a) Find the maximum element quickly. :(b) Delete an element quickly. :(c) Form the structure quickly. :(d) Find the minimum element quickly. [[4.19|Solution]] :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? ===Quicksort=== :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) [[4.21|Solution]] :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. [[4.23|Solution]] :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? [[4.25|Solution]] :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. [[4.27|Solution]] ===Mergesort=== :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? [[4.29|Solution]] :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? ===Other Sorting Alogrithims=== :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. [[4.31|Solution]] :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. [[4.33|Solution]] :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. [[4.35|Solution]] :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. [[4.37|Solution]] :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. ===Lower Bounds=== :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in <math>O(n log(\sqrt n))</math>. Given the existence of an <math>\Omega(n log n)</math> lower bound for sorting, how can this be possible? [[4.39|Solution]] :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in <math>O(1)</math> worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the <math>\Omega (n log n)</math> lower bound for sorting.) ===Searching=== :[[4.41]]. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database: :• Put all the names in a single array and use binary search. :• Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array. :Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options? [[4.41|Solution]] :4.42. A ''Ramanujan number'' can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers <math>a, b, c</math>, and <math>d</math> such that <math>a^3 + b^3 = c^3 + d^3</math>. For example, 1729 is a Ramanujan number because <math>1729 = 1^3 + 12^3 = 9^3 + 10^3</math>. :(a) Give an efficient algorithm to test whether a given single integer <math>n</math> is a Ramanujan number, with an analysis of the algorithm’s complexity. :(b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and <math>n</math>, with an analysis of its complexity. ===Implementaion Challenges=== :[[4.43]]. Consider an n×n array <math>A</math> containing integer elements (positive, negative, and zero). Assume that the elements in each row of <math>A</math> are in strictly increasing order, and the elements of each column of <math>A</math> are in strictly decreasing order. (Hence there cannot be two zeros in the same row or the same column.) Describe an efficient algorithm that counts the number of occurrences of the element 0 in <math>A</math>. Analyze its running time. [[4.43|Solution]] :4.44. Implement versions of several different sorting algorithms, such as selection sort, insertion sort, heapsort, mergesort, and quicksort. Conduct experiments to assess the relative performance of these algorithms in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by sorting all the words that occur in the text and then passing through the sorted sequence to identify one instance of each distinct word. Write a brief report with your conclusions. :[[4.45]]. Implement an external sort, which uses intermediate files to sort files bigger than main memory. Mergesort is a good algorithm to base such an implementation on. Test your program both on files with small records and on files with large records. [[4.45|Solution]] :4.46. Design and implement a parallel sorting algorithm that distributes data across several processors. An appropriate variation of mergesort is a likely candidate. Measure the speedup of this algorithm as the number of processors increases. Then compare the execution time to that of a purely sequential mergesort implementation. What are your experiences? ===Interview Problems=== :[[4.47]]. If you are given a million integers to sort, what algorithm would you use to sort them? How much time and memory would that consume? [[4.47|Solution]] :4.48. Describe advantages and disadvantages of the most popular sorting algorithms. :[[4.49]]. Implement an algorithm that takes an input array and returns only the unique elements in it. [[4.49|Solution]] :4.50. You have a computer with only 4 GB of main memory. How do you use it to sort a large file of 500 GB that is on disk? :[[4.51]]. Design a stack that supports push, pop, and retrieving the minimum element in constant time. [[4.51|Solution]] :4.52. Given a search string of three words, find the smallest snippet of the document that contains all three of the search words—that is, the snippet with the smallest number of words in it. You are given the index positions where these words occur in the document, such as ''word1'': <math>(1, 4, 5)</math>, ''word2'': <math>(3, 9, 10)</math>, and ''word3'': <math>(2, 6, 15)</math>. Each of the lists are in sorted order, as above. :[[4.53]]. You are given twelve coins. One of them is heavier or lighter than the rest. Identify this coin in just three weighings with a balance scale. [[4.53|Solution]] Back to [[Chapter List]] e58fc8593883cda05ec646236632e3a442dbc043 Chapter 5 0 8 450 253 2020-10-01T18:08:24Z Algowikiadmin 1 Protected "[[Chapter 5]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Divide and Conquer= ===Binary Search=== :[[5.1]]. Suppose you are given a sorted array <math>A</math> of size n that has been circularly shifted <math>k</math> positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted <math>k = 2</math> positions, while [27, 29, 35, 42, 5, 15] has been shifted <math>k = 4</math> positions. :• Suppose you know what <math>k</math> is. Give an <math>O(1)</math> algorithm to find the largest number in <math>A</math>. :• Suppose you do not know what <math>k</math> is. Give an <math>O(lg n)</math> algorithm to find the largest number in <math>A</math>. For partial credit, you may give an <math>O(n)</math> algorithm. [[5.1|Solution]] :5.2. A sorted array of size n contains distinct integers between <math>1</math> and <math>n + 1</math> , with one element missing. Give an <math>O(log n)</math> algorithm to find the missing integer, without using any extra space. :[[5.3]] Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to <math>n</math>. The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats. :(a) What is an optimal strategy if <math>n</math> in known? :(b) What is a good strategy if <math>n</math> is not known? [[5.3|Solution]] :5.4. You are given a unimodal array of <math>n</math> distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in <math>O(log n)</math> time. :[[5.5]]. Suppose that you are given a sorted sequence of distinct integers <math>[a_1, a_2, . . . , a_n]</math>. Give an <math>O(lg n)</math> algorithm to determine whether there exists an index <math>i</math> such that <math>a_i = i</math>. For example, in [-10, -3, 3, 5, 7], <math>a_3 = 3</math>. In [2, 3, 4, 5, 6, 7], there is no such <math>i</math>. [[5.5|Solution]] :5.6. Suppose that you are given a sorted sequence of distinct integers <math>a = [a_1, a_2, . . . , a_n]</math>, drawn from 1 to <math>m</math> where <math>n < m</math>. Give an <math>O(lg n)</math> algorithm to find an integer <math>\leq m</math> that is not present in <math>a</math>. For full credit, find the smallest such integer <math>x</math> such that <math>1 \leq x \leq m</math>. :[[5.7]]. Let <math>M</math> be an <math>n * m</math> integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer <math>x</math> in <math>M</math>, or to determine that <math>x</math> is not there. How many comparisons of <math>x</math> with matrix entries does your algorithm use in worst case? [[5.7|Solution]] ===Divide and Conquer Algorithms=== :5.8. Given two sorted arrays <math>A</math> and <math>B</math> of size <math>n</math> and <math>m</math> respectively, find the median of the <math>n + m</math> elements. The overall run time complexity should be <math>O(log(m + n))</math>. :[[5.9]]. The largest subrange problem, discussed in Section 5.6, takes an array <math>A</math> of <math>n</math> numbers, and asks for the index pair <math>i</math> and <math>j</math> that maximizes <math>S = \sum_{k=1}^j A[k]</math>. Give an <math>O(n)</math> algorithm for largest subrange. [[5.9|Solution]] :5.10. We are given <math>n</math> wooden sticks, each of integer length, where the <math>i</math>th piece has length <math>L[i]</math>. We seek to cut them so that we end up with <math>k</math> pieces of exactly the same length, in addition to other fragments. Furthermore, we want these <math>k</math> pieces to be as large as possible. :(a) Given four wood sticks, of lengths <math>L = {10, 6, 5, 3}</math>, what are the largest sized pieces you can get for <math>k = 4</math>? (Hint: the answer is not 3). :(b) Give a correct and efficient algorithm that, for a given <math>L</math> and <math>k</math>, returns the maximum possible length of the <math>k</math> equal pieces cut from the initial <math>n</math> sticks. :[[5.11]]. Extend the convolution-based string-matching algorithm described in the text to the case of pattern matching with wildcard characters “*”, which match any character. For example, “sh*t” should match both “shot” and “shut”. [[5.11|Solution]] ===Recurrence Relations=== :5.12. In Section 5.3, it is asserted that any polynomial can be represented by a recurrence. Find a recurrence relation that represents the polynomial <math>a_n = n^2</math>. :[[5.13]]. Suppose you are choosing between the following three algorithms: :• Algorithm <math>A</math> solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time. :• Algorithm <math>B</math> solves problems of size <math>n</math> by recursively solving two subproblems of size <math>n - 1</math> and then combining the solutions in constant time. :• Algorithm <math>C</math> solves problems of size <math>n</math> by dividing them into nine subproblems of size <math>n/3</math>, recursively solving each subproblem, and then combining the solutions in <math>\Theta(n^2)</math> time. :What are the running times of each of these algorithms (in big O notation), and which would you choose? [[5.13|Solution]] :5.14. Solve the following recurrence relations and give a <math>\Theta</math> bound for each of them: :(a) <math>T(n) = 2T(n/3) + 1</math> :(b) <math>T(n) = 5T(n/4) + n</math> :(c) <math>T(n) = 7T(n/7) + n</math> :(d) <math>T(n) = 9T(n/3) + n^2</math> :[[5.15]]. Use the master theorem to solve the following recurrence relations: :(a) <math>T(n) = 64T(n/4) + n^4</math> :(b) <math>T(n) = 64T(n/4) + n^3</math> :(c) <math>T(n) = 64T(n/4) + 128</math> [[5.15|Solution]] :5.16. Give asymptotically tight upper (Big Oh) bounds for <math>T(n)</math> in each of the following recurrences. Justify your solutions by naming the particular case of the master theorem, by iterating the recurrence, or by using the substitution method: :(a) <math>T(n) = T(n - 2) + 1</math>. :(b) <math>T(n) = 2T(n/2) + n lg^2 n</math>. :(c) <math>T(n) = 9T(n/4) + n^2</math>. Back to [[Chapter List]] a49064d6456db1c54581ee3dbb9ce9a253fed75d Chapter 6 0 9 451 250 2020-10-01T18:08:44Z Algowikiadmin 1 Protected "[[Chapter 6]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Hashing and Randomized Algorithms= ===Probability=== :[[6.1]]. You are given <math>n</math> unbiased coins, and perform the following process to generate all heads. Toss all <math>n</math> coins independently at random onto a table. Each round consists of picking up all the tails-up coins and tossing them onto the table again. You repeat until all coins are heads. :(a) What is the expected number of rounds performed by the process? :(b) What is the expected number of coin tosses performed by the process? [[6.1|Solution]] :6.2. Suppose we flip <math>n</math> coins each of known bias, such that <math>p_i</math> is the probability of the <math>i</math>th coin being a head. Present an efficient algorithm to determine the exact probability of getting exactly <math>k</math> heads given <math>p_1, . . . , p_n \in [0, 1]</math>. :[[6.3]]. An inversion of a permutation is a pair of elements that are out of order. :(a) Show that a permutation of <math>n</math> items has at most <math>n(n - 1)/2</math> inversions. Which permutation(s) have exactly n(n - 1)/2 inversions? :(b) Let P be a permutation and <math>P^r</math> be the reversal of this permutation. Show that <math>P</math> and <math>P^r</math> have a total of exactly <math>n(n - 1)/2</math> inversions. :(c) Use the previous result to argue that the expected number of inversions in a random permutation is <math>n(n - 1)/4</math>. [[6.3|Solution]] :6.4. A derangement is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i \neq i</math> for all <math>1 \leq i \leq n</math>. What is the probability that a random permutation is a derangement? ===Hashing=== :[[6.5]]. An all-Beatles radio station plays nothing but recordings by the Beatles, selecting the next song at random (uniformly with replacement). They get through about ten songs per hour. I listened for 90 minutes before hearing a repeated song. Estimate how many songs the Beatles recorded. [[6.5|Solution]] :6.6. Given strings <math>S</math> and <math>T</math> of length <math>n</math> and <math>m</math> respectively, find the shortest window in <math>S</math> that contains all the characters in <math>T</math> in expected <math>O(n + m)</math> time. :[[6.7]]. Design and implement an efficient data structure to maintain a ''least recently used'' (LRU) cache of <math>n</math> integer elements. A LRU cache will discard the least recently accessed element once the cache has reached its capacity, supporting the following operations: :• ''get(k)''– Return the value associated with the key <math>k</math> if it currently exists in the cache, otherwise return -1. :• ''put(k,v)'' – Set the value associated with key <math>k</math> to <math>v</math>, or insert if <math>k</math> is not already present. If there are already <math>n</math> items in the queue, delete the least recently used item before inserting <math>(k, v)</math>. Both operations should be done in <math>O(1)</math> expected time. [[6.7|Solution]] ===Randomized Algorithms=== :6.8. A pair of English words <math>(w_1, w_2)</math> is called a ''rotodrome'' if one can be circularly shifted (rotated) to create the other word. For example, the words (windup, upwind) are a rotodrome pair, because we can rotate “windup” two positions to the right to get “upwind.” :Give an efficient algorithm to find all rotodrome pairs among <math>n</math> words of length <math>k</math>, with a worst-case analysis. Also give a faster expected-time algorithm based on hashing. :[[6.9]]. Given an array <math>w</math> of positive integers, where <math>w[i]</math> describes the weight of index <math>i</math>, propose an algorithm that randomly picks an index in proportion to its weight. [[6.9|Solution]] :6.10. You are given a function ''rand7'', which generates a uniform random integer in the range 1 to 7. Use it to produce a function ''rand10'', which generates a uniform random integer in the range 1 to 10. :[[6.11]]. Let <math>0 < \alpha < 1/2</math> be some constant, independent of the input array length <math>n</math>. What is the probability that, with a randomly chosen pivot element, the partition subroutine from quicksort produces a split in which the size of both the resulting subproblems is at least <math>\alpha</math> times the size of the original array? [[6.11|Solution]] :6.12. Show that for any given load <math>m/n</math>, the error probability of a Bloom filter is minimized when the number of hash functions is <math>k = exp(-1)/(m/n)</math>. Back to [[Chapter List]] 72b1caa31cac5e1dd82ce0b7a9b5c4dcff357f7c Chapter 7 0 10 452 358 2020-10-01T18:08:55Z Algowikiadmin 1 Protected "[[Chapter 7]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38. An articulation vertex of a connected graph <math>G</math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) [[7.39|Solution]] :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. [[7.41|Solution]] ===Interview Problems=== :7.42. Which data structures are used in depth-first and breath-first search? :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order. [[7.43|Solution]] Back to [[Chapter List]] 9f4c96c52d5502139b2816415e3c970dd181b035 453 452 2020-10-01T18:09:33Z Algowikiadmin 1 /* Simulating Graph Algorithms */ wikitext text/x-wiki =Graph Traversal= ===Simulating Graph Algorithms=== :[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right): :(see book for figures) :(a) Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). :(b) Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). [[7.1|Solution]] :7.2. Do a topological sort of the following graph <math>G</math>: :(see book for figure) ===Traversal=== :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree. [[7.3|Solution]] :7.4. Prove that in a breadth-first search on a undirected graph <math>G</math>, every edge is either a tree edge or a cross edge, where <math>x</math> is neither an ancestor nor descendant of <math>y</math> in cross edge <math>(x, y)</math>. :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite? [[7.5|Solution]] :7.6. You are given a connected, undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. Give an <math>O(n + m)</math> algorithm to identify an edge you can remove from <math>G</math> while still leaving <math>G</math> connected, if one exists. Can you reduce the running time to <math>O(n)</math>? :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state. :(a) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from <math>v</math>. :(b) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that <math>\Theta(n)</math> nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from <math>v</math>. :(c) Describe a graph on <math>n</math> vertices and a particular starting vertex <math>v</math> such that at some point <math>\Theta(n)</math> nodes remain ''undiscovered'', while <math>\Theta(n)</math> nodes have been ''processed'' during a ''depth-first search'' starting from <math>v</math>.(Note, there may also be ''discovered'' nodes.) [[7.7|Solution]] :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals. :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph <math>G</math> between the following graph data structures. You must give the time complexity of each algorithm, assuming <math>n</math> vertices and <math>m</math> edges. :(a)Convert from an adjacency matrix to adjacency lists. :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix <math>M</math> has a row for each vertex and a column for each edge, such that <math>M[i,j]=1</math> if vertex <math>i</math> is part of edge <math>j</math>, otherwise <math>M[i,j] = 0</math>. :(c)Convert from an incidence matrix to adjacency lists. [[7.9|Solution]] :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+, -, *, /)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the tree in Figure 7.17(a). Give an <math>O(n)</math> algorithm for evaluating such an expression, where there are <math>n</math> nodes in the tree. :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations <math>(+,-,*,/)</math>. For example, the expression <math>2+3*4+(3*4)/5</math> is represented by the DAG in Figure (see book)(b). Give an <math>O(n+m)</math> algorithm for evaluating such a DAG, where there are <math>n</math> nodes and <math>m</math> edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency. [[7.11|Solution]] :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem. ===Applications=== :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. [[7.13|Solution]] :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. [[7.15|Solution]] ===Algorithm Design=== :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. :Give efficient algorithms for both adjacency lists and matrices. :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. [[7.17|Solution]] :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. [[7.19|Solution]] :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? :[[7.21]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. [[7.21|Solution]] :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by <center><math>\max_{u,v\in V} \delta(u,v)</math></center> :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. [[7.23|Solution]] :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. [[7.25|Solution]] :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. ===Directed Graphs=== :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. [[7.27|Solution]] :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? [[7.29|Solution]] :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? [[7.31|Solution]] :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. [[7.33|Solution]] :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. [[7.35|Solution]] :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. [[7.37|Solution]] ===Articulation Vertices=== :7.38. An articulation vertex of a connected graph <math>G</math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) [[7.39|Solution]] :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. [[7.41|Solution]] ===Interview Problems=== :7.42. Which data structures are used in depth-first and breath-first search? :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order. [[7.43|Solution]] Back to [[Chapter List]] b00058d4d5067d2e14d982a7165ed679ced823bf Chapter 8 0 11 454 433 2020-10-01T18:09:48Z Algowikiadmin 1 Protected "[[Chapter 8]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Weighted Graph Algorithms= ===Simulating Graph Algorithms=== :[[8.1]]. For the graphs in Problem 7-1: :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm. :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm. :(c) Find the shortest-path spanning tree rooted in <math>A</math>. :(d) Compute the maximum flow from <math>A</math> to <math>H</math>. [[8.1|Solution]] ===Minimum Spanning Tree=== :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample. [[8.3|Solution]] :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not. :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not. [[8.5|Solution]] :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample. :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample. :[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit. [[8.7|Solution]] :8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. :(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample. :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time. [[8.9|Solution]] :8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>. :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.) :(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>. :[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree: :(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math> :(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math> :(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math> :(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math> [[8.11|Solution]] :8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>. :(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set. :(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set. ===Union Find=== :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph: :(a)Merge two given components. :(b)Locate which component contains a given vertex <math>v</math>. :(c)Retrieve a minimum edge from a given component. [[8.13|Solution]] :8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>. ===Shortest Paths=== :[[8.15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem. [[8.15|Solution]] :8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>? :[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>. :(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>. :(c)Can the two trees be completely disjointed? [[8.17|Solution]] :8.18. Either prove the following or give a counterexample: :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path? :[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>. [[8.19|Solution]] :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path. :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. :[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph. [[8.21|Solution]] :8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm? :[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time. [[8.23|Solution]] :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample. :[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>. [[8.25|Solution]] :8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple. :[[8.27]]. ''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path. [[8.27|Solution]] ===Network Flow and Matching=== :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree. :[[8.29]]. An edge cover of an undirected graph <math>G = (V, E)</math> is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for <math>G</math>. [[8.29|Solution]] Back to [[Chapter List]] 77e827584b838786246004c28b13ef07dd2ec18c Chapter 9 0 12 455 408 2020-10-01T18:10:14Z Algowikiadmin 1 Protected "[[Chapter 9]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Combinatorial Search= ===Permutations=== :[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items. [[9.1|Soluiton]] :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset. :[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3]. [[9.3|Solution]] :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time. :[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</math>: <center><math>[123, 132, 213, 231, 312, 321]</math></center> :Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math> permutations in the process. [[9.5|Solution]] ===Backtracking=== :9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>. :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses. [[9.7|Solution]] :9.8. Generate all possible topological orderings of a given DAG. :[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. [[9.9|Solution]] :9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum. :[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>. :(a) List the 10 possible team assignments for <math>n = 6</math> players. :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution. [[9.11|Solution]] :9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists. :[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem. [[9.13|Solution]] :9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight. :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time. [[9.15|Solution]] ===Games and Puzzles=== :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary. :[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once. [[9.17|Solution]] :9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board: <center><math>\begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix}</math></center> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>. :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are: <center><math>\begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} </math></center> [[9.19|Solution]] :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph. ===Combinatorial Optimization=== For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice? :[[9.21]]. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470). [[9.21|Solution]] :9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537). :[[9.23]]. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586). [[9.23|Solution]] :9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604). :[[9.25]]. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608). [[9.25|Solution]] :9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618). :[[9.27]]. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678). [[9.27|Solution]] ===Interview Problems=== :9.28. Write a function to find all permutations of the letters in a given string. :[[9.29]]. Implement an efficient algorithm for listing all <math>k</math>-element subsets of <math>n</math> items. [[9.29|Solution]] :9.30. An anagram is a rearrangement of the letters in a given string into a sequence of dictionary words, like ''Steven Skiena'' into ''Vainest Knees''. Propose an algorithm to construct all the anagrams of a given string. :[[9.31]]. Telephone keypads have letters on each numerical key. Write a program that generates all possible words resulting from translating a given digit sequence (e.g. 145345) into letters. [[9.31|Solution]] :9.32. You start with an empty room and a group of <math>n</math> people waiting outside. At each step, you may either admit one person into the room, or let one out. Can you arrange a sequence of <math>2n</math> steps, so that every possible combination of people is achieved exactly once? :[[9.33]]. Use a random number generator (rng04) that generates numbers from {0, 1, 2, 3, 4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability. What is the expected number of calls to rng04 per call of rng07? [[9.33|Solution]] Back to [[Chapter List]] 4bcea818ad0982cfdad52cbf0797c83ee0fbf052 Chapter 10 0 13 456 394 2020-10-01T18:10:29Z Algowikiadmin 1 Protected "[[Chapter 10]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Dynamic Programming= ===Elementary Recurrences=== :[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm? [[10.1|Solution]] :10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police. :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1). [[10.3|Solution]] :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1. :[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time. ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>? ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>? [[10.5|Solution]] ===Edit Distance=== :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance. :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation. :[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string. :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not. :(b) Give an efficient dynamic programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.) [[10.7|Solution]] :10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph. :(a) Let <math>n = |X|</math> and <math>m = |Y|</math>. Give a <math>\Theta (nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm. :(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming. :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>. :(a) Give efficient algorithms to find the LCS and SCS of two given sequences. :(b) Let <math>d(T, P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T, P) = |SCS(T, P)| - |LCS(T, P)|</math> where <math>|SCS(T, P)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>. [[10.9|Solution]] :10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. :For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles. ===Greedy Algorithms=== :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. [[10.11|Solution]] :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. :(a) How many ways are there to make change of 20 units from {1, 6, 10}? :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) [[10.13|Solution]] :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. ===Number Problems=== :[[10.15]]. You are given a rod of length <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are: <center><math> \begin{array}{|c|c|c|c|c|c|c|c||c|} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\ \hline price&1&5&8&9&10&17&17&20\\ \end{array}</math> </center> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6. [[10.15|Solution]] :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression: <center><math>6 + 2 * 0 - 4</math></center> :there exist parenthesizations with values ranging from −32 to 2. :[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm? [[10.17|Solution]] :10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions. :[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible. [[10.19|Solution]] :10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>. :Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time. :[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that <center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center> :Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem. [[10.21|Solution]] :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem. :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units. :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time. [[10.23|Solution]] :10.24. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table. :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as <math>1/2</math>. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black. :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to get <math>i</math> points (which may be a multiple of <math>1/2</math>). :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title. :(c) Analyze its running time for an <math>n</math> game match. [[10.25|Solution]] :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>. :You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice. :(a) Show that <math>E(1, n) = n</math>. :(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>. :(c) Find a recurrence for <math>E(k, n)</math>. What is the running time of the dynamic program to find <math>E(k, n)</math>? ===Graphing Problem=== :[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods. :(b) Give an <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods. :(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm. [[10.27|Solution]] :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. :Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>. :[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack. [[10.29|Solution]] ===Design Problems=== :10.30. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. :Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity. :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf. :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.) [[10.31|Solution]] :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank. :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard. :[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one. [[10.33|Solution]] :10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''. :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time. :(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time. :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be. [[10.35|Solution]] :10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array <center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0. #Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray. #Now give a <math>\Theta (n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n log n)</math> divide-and-conquer algorithm. :[[10.37]]. Consider the problem of examining a string <math>x = x_1 x_2 \ldots x_n</math> from an alphabet of <math>k</math> symbols, and a multiplication table over this alphabet. Decide whether or not it is possible to parenthesize <math>x</math> in such a way that the value of the resulting expression is <math>a</math>, where <math>a</math> belongs to the alphabet. The multiplication table is neither commutative or associative, so the order of multiplication matters. <center> <math>\begin{array}{c|ccc} & a & b & c \\ \hline a & a & c & c \\ b & a & a & b \\ c & c & c & c \\ \end{array}</math> </center> :For example, consider the above multiplication table and the string <math>bbbba</math>. Parenthesizing it <math>(b(bb))(ba)</math> gives <math>a</math>, but <math>((((bb)b)b)a)</math> gives <math>c</math>. :Give an algorithm, with time polynomial in <math>n</math> and <math>k</math>, to decide whether such a parenthesization exists for a given string, multiplication table, and goal element. [[10.37|Solution]] :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>. ===Interview Problems=== :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change. [[10.39|Solution]] :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers. :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position. [[10.41|Solution]] Back to [[Chapter List]] 4f80f74e096f653db069e918e7bd78701dd82865 Chapter 11 0 14 457 204 2020-10-01T18:10:47Z Algowikiadmin 1 Protected "[[Chapter 11]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =NP-Completeness= ===Transformations and Satisfiability=== :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: :::<math> (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) </math> [[11.1|Solution]] :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression :::<math>(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) </math> :[[11.3]]. Prove that 4-SAT is NP-hard. [[11.3|Solution]] :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer <math>k</math>, find a satisfying assignment in which at most <math>k</math> variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard. :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem <math>{{v1, v2}, {v_1, v_2}, {v_1, v_2}}</math> is satisfiable, but has only one solution <math>(v_1 =F, v_2 = T)</math>. In contrast, <math>{{v_1, v_2}, {v_1, v_2}}</math> has exactly two solutions. Show that Double-SAT is NP-hard. [[11.5|Solution]] :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine. :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances. [[11.7|Solution]] :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search? :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things? [[11.9|Solution]] ===Basic Reductions=== :11.10. An instance of the ''set cover'' problem consists of a set <math>X</math> of <math>n</math> elements, a family <math>F</math> of subsets of <math>X</math>, and an integer <math>k</math>. The question is, does there exist <math>k</math> subsets from <math>F</math> whose union is <math>X</math>? For example, if <math>X = \{1,2,3,4\}</math> and <math>F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}</math>, there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math> (for example, <math> \{1,2\}, \{2,3\}, \{4\}</math>). Prove that set cover is NP-complete with a reduction from vertex cover. :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets <math>P_1, \ldots, P_m</math>, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying <math>\leq k</math> packets? For example, if the players are <math> \{Aaron, Mays, Ruth, Steven \} </math> and the packets are :::<math> \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, </math> :there does not exist a solution for <math>k=2</math>, but there does for <math>k=3</math>, such as :::<math> \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} </math> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover. [[11.11|Solution]] :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree such that all vertices in the tree have degree ''at most'' <math>k</math> (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three. \fixedfigsize{pictures/lowdegree.png}{1.0in} #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph <math>G</math> and an integer <math>k</math>, does <math>G</math> contain a spanning tree whose highest degree vertex is ''at least'' <math>k</math>? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity. :[[11.13]].In the minimum element set cover problem, we seek a set cover <math>S \subseteq C</math> of a universal set <math>U = {1, . . . , n}</math> such that sum of the sizes of the subsets in <math>S</math> is at most <math>k</math>. ::(a) Show that <math>C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}</math> has a cover of size 6, but none of size 5 because of a repeated element. ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.) [[11.13|Solution]] :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph <math>G</math> with <math>n</math> vertices, determine whether <math>G</math> has a simple cycle of length exactly <math>[n/2]</math>, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard. :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets <math>A</math>, <math>B</math>, or <math>C</math> such that <math>\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i</math>. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)). [[11.15|Solution]] :11.16. Show that the following problem is NP-complete: :* ''Problem:'' Dense subgraph :* ''Input:'' A graph <math>G</math>, and integers <math>k</math> and <math>y</math>. :* ''Output:'' Does <math>G</math> contain a subgraph with exactly <math>k</math> vertices and at least <math>y</math> edges? :[[11.17]]. Show that the following problem is NP-complete: :* ''Problem:'' Clique, no-clique :* ''Input:'' An undirected graph <math>G=(V,E)</math> and an integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain both a clique of size <math>k</math> and an independent set of size <math>k</math>. [[11.17|Solution]] :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.) :[[11.19]]. Show that the following problem is NP-hard: :*''Problem:'' Maximum Common Subgraph :*''Input:'' Two graphs <math>G_1 = (V_1, E_1)</math> and <math>G_2 = (V_2, E_2)</math>, and a budget <math>b</math>. :*Output: Two sets of nodes <math>S_1 \subseteq V_1</math> and <math>S_2 \subseteq V_2</math> whose deletion leaves at least <math>b</math> nodes in each graph, and makes the two graphs identical. [[11.19|Solution]] :11.20. A ''strongly independent'' set is a subset of vertices <math>S</math> in a graph <math>G</math> such that for any two vertices in <math>S</math>, there is no path of length two in <math>G</math>. Prove that strongly independent set is NP-hard. :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say <math>2n</math>, in which <math>n</math> of the vertices form a clique and the remaining <math>n</math> vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains <math>2g</math> nodes. Prove that ''max kite'' is NP-hard. [[11.21|Solution]] ===Creative Reductions=== :11.22. Prove that the following problem is NP-complete: :* ''Problem:'' Hitting Set :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain a subset <math>S'</math> such that <math>|S'| \leq k</math> and each subset in <math>C</math> contains at least one element from <math>S'</math>? :[[11.23]]. Prove that the following problem is NP-complete: :* ''Problem:'' Knapsack :* ''Input:'' A set <math>S</math> of <math>n</math> items, such that the <math>i</math>th item has value <math>v_i</math> and weight <math>w_i</math>. Two positive integers: weight limit <math>W</math> and value requirement <math>V</math>. :* ''Output:'' Does there exist a subset <math>S' \in S</math> such that <math>\sum_{i \in S'} w_i \leq W</math> and <math>\sum_{i \in S'} v_i \geq V</math>? :(Hint: start from integer partition.) [[11.23|Solution]] :11.24. Prove that the following problem is NP-complete: :* ''Problem:'' Hamiltonian Path :* ''Input:'' A graph <math>G</math>, and vertices <math>s</math> and <math>t</math>. :* ''Output:'' Does <math>G</math> contain a path which starts from <math>s</math>, ends at <math>t</math>, and visits all vertices without visiting any vertex more than once? (Hint: start from Hamiltonian cycle.) :[[11.25]]. Prove that the following problem is NP-complete: :* ''Problem:'' Longest Path :* ''Input:'' A graph <math>G</math> and positive integer <math>k</math>. :* ''Output:'' Does <math>G</math> contain a path that visits at least <math>k</math> different vertices without visiting any vertex more than once? [[11.25|Solution]] :11.26. Prove that the following problem is NP-complete: :* ''Problem:'' Dominating Set :* ''Input:'' A graph <math>G=(V,E)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math> where for each vertex <math>x \in V</math> either <math>x \in V'</math> or there exists an edge <math>(x,y)</math>, where <math>y \in V'</math>. :[[11.27]]. Prove that the vertex cover problem (does there exist a subset <math>S</math> of <math>k</math> vertices in a graph <math>G</math> such that every edge in <math>G</math> is incident upon at least one vertex in <math>S</math>?) remains NP-complete even when all the vertices in the graph are restricted to have even degrees. [[11.27|Solution]] :11.28. Prove that the following problem is NP-complete: :* ''Problem:'' Set Packing :* ''Input:'' A collection <math>C</math> of subsets of a set <math>S</math>, positive integer <math>k</math>. :* ''Output:'' Does <math>S</math> contain at least <math>k</math> disjoint subsets (i.e., such that none of these subsets have any elements in common?) :[[11.29]]. Prove that the following problem is NP-complete: :* ''Problem:'' Feedback Vertex Set :* ''Input:'' A directed graph <math>G=(V,A)</math> and positive integer <math>k</math>. :* ''Output:'' Is there a subset <math>V' \in V</math> such that <math>|V'|\leq k</math>, such that deleting the vertices of <math>V'</math> from <math>G</math> leaves a DAG? [[11.29|Solution]] :11.30. Give a reduction from Sudoku to the vertex coloring problem in graphs. Specifically, describe how to take any partially filled Sudoku board and construct a graph that can be colored with nine colors iff the Sudoku board is solvable. ===Algorithms for Special Cases=== :[[11.31]]. A Hamiltonian path <math>P</math> is a path that visits each vertex exactly once. The problem of testing whether a graph <math>G</math> contains a Hamiltonian path is NP-complete. There does not have to be an edge in <math>G</math> from the ending vertex to the starting vertex of <math>P</math>, unlike in the Hamiltonian cycle problem. Give an <math>O(n+m)</math>-time algorithm to test whether a directed acyclic graph <math>G</math> (a DAG) contains a Hamiltonian path. (Hint: think about topological sorting and DFS.) [[11.31|Solution]] :11.32. Consider the ''k''-clique problem, which is the general clique problem restricted to graphs in which every vertex has degree at most <math>k</math>. Prove that ''k''-clique has an efficient algorithm for any given <math>k</math>, meaning that <math>k</math> is a constant. :[[11.33]]. The <math>2</math>-SAT problem is, given a Boolean formula in 2-conjunctive normal form (CNF), to decide whether the formula is satisfiable. <math>2</math>-SAT is like <math>3</math>-SAT, except that each clause can have only two literals. For example, the following formula is in <math>2</math>-CNF: <math> (x_1 \or x_2) \and (\bar{x}_2 \or x_3) \and (x_1 \or \bar{x}_3) </math> :Give a polynomial-time algorithm to solve <math>2</math>-SAT. [[11.33|Solution]] :11.34, Show that the following problems are in NP: #Does graph <math>G</math> have a simple path (i.e., with no vertex repeated) of length <math>k</math>? \#Is integer <math>n</math> composite (i.e., not prime)? #Does graph <math>G</math> have a vertex cover of size <math>k</math>? :[[11.35]]. Until 2002, it was an open question whether the decision problem ''Is integer <math>n</math> a composite number, in other words, not prime?'' can be computed in time polynomial in the size of its input. Why doesn't the following algorithm suffice to prove it is in P, since it runs in <math>O(n)</math> time? PrimalityTesting(<math>n</math>) composite := <math>false</math> for i := 2 to <math>n-1</math> do if <math>(n\,\bmod\,i) = 0</math> then composite := <math>true</math> [[11.35|Solution]] Back to [[Chapter List]] 14c9bd71fc58f891913e1d6ec63d0417d87f5fc4 Chapter 12 0 15 458 179 2020-10-01T18:11:07Z Algowikiadmin 1 Protected "[[Chapter 12]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Dealing with Hard Problems= ===Special Cases of Hard Problems=== :[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>. ::(a) Prove that finding the longest domino chain is NP-complete. ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>. [[12.1|Solution]] :12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>. ::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens. ::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG). :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian. [[12.3|Solution]] ===Approximation Algorithms=== :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution. :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal. [[12.5|Solution]] :12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut. :[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram. :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution. [[12.7|Solution]] :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal. :[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set. [[12.9|Solution]] :12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>. :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph. [[12.11|Solution]] ===Combinatorial Optimization=== For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice? :12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470). :[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537). [[12.13|Solution]] :12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586). :[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604). [[12.15|Solution]] :12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608). :[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618). [[12.17|Solution]] :12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678). ==="Quantum" Computing=== :[[12.19]]. Consider an <math>n</math> qubit “quantum” system <math>Q</math>, where each of the <math>N = 2^n</math> states start out with equal probability <math>p(i) = 1/2^n</math>. Say the ''Jack''<math>(Q, 0^n)</math> operation doubles the probability of the state where all qubits are zero. How many calls to this ''Jack'' operation are necessary until the probability of sampling this null state becomes ≥ 1/2? [[12.19|Solution]] :12.20. For the satisfiability problem, construct (a) an instance on <math>n</math> variables that has exactly one solution, and (b) an instance on <math>n</math> variables that has exactly <math>2^n</math> different solutions. :[[12.21]]. Consider the first ten multiples of 11, namely 11, 22, . . . 110. Pick two of them (<math>x</math> and <math>y</math>) at random. What is the probability that ''gcd''<math>(x, y) = 11</math>? [[12.21|Solution]] :12.22. IBM quantum computing (https://www.ibm.com/quantum-computing/) offers the opportunity to program a quantum computing simulator. Take a look at an example quantum computing program and run it to see what happens. Back to [[Chapter List]] 9ed3ef2cfc1f3ebac226557eb19791450473c8e7 Chapter 2 0 4 459 265 2020-10-01T18:11:11Z Algowikiadmin 1 Protected "[[Chapter 2]]" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) wikitext text/x-wiki =Algorithm Analysis= ===Program Analysis=== :[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation. mystery(''n'') r:=0 ''for'' i:=1 ''to'' n-1 ''do'' ''for'' j:=i+1 ''to'' n ''do'' ''for'' k:=1 ''to'' j ''do'' r:=r+1 ''return''(r) [[2.1|Solution]] :2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. pesky(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' r:=r+1 ''return''(r) :[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. prestiferous(n) r:=0 ''for'' i:=1 ''to'' n ''do'' ''for'' j:=1 ''to'' i ''do'' ''for'' k:=j ''to'' i+j ''do'' ''for'' l:=1 ''to'' i+j-k ''do'' r:=r+1 ''return''(r) [[2.3|Solution]] :2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation. conundrum(<math>n</math>) <math>r:=0</math> ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do'' ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do'' ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do'' <math>r:=r+1</math> ''return''(r) :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>). ''for'' <math> k = 1</math> to <math>n</math> <math>x = k</math> ''while'' (<math>x < n</math>): ''print'' '*' <math>x = 2x</math> :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>\Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. [[2.5|Solution]] :2.6. Suppose the following algorithm is used to evaluate the polynomial ::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math> <math>p:=a_0;</math> <math>xpower:=1;</math> for <math>i:=1</math> to <math>n</math> do <math>xpower:=x*xpower;</math> <math>p:=p+a_i * xpower</math> #How many multiplications are done in the worst-case? How many additions? #How many multiplications are done on the average? #Can you improve this algorithm? :2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct. max(A) <math>m:=A[1]</math> ''for'' <math>i:=2</math> ''to'' n ''do'' ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math> ''return'' (m) [[2.7|Solution]] ===Big Oh=== :2.8. True or False? #Is <math>2^{n+1} = O (2^n)</math>? #Is <math>2^{2n} = O(2^n)</math>? :[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why. #<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math> #<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math> #<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math> #<math>f(n)=n</math>; <math>g(n)=\log^2 n</math> #<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math> #<math>f(n)=10</math>; <math>g(n)=\log 10</math> #<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math> #<math>f(n)=2^n</math>; <math>g(n)=3^n</math> [[2.9|Solution]] :2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both. #<math>f(n) = (n^2 - n)/2</math>, <math>g(n) =6n</math> #<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math> #<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math> #<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math> #<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math> #<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? #<math>f(n) = 3n^2, g(n) = n^2</math> #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> #<math>f(n) = log n, g(n) = log n + 1/n</math> #<math>f(n) = 2^{klog n}, g(n) = n^k</math> #<math>f(n) = 2^n, g(n) = 2^{2n}</math> [[2.11|Solution]] :2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>. :2.13. Prove that <math>n^2 = O(2^n)</math>. [[2.13|Solution]] :2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>. :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> [[2.15|Solution]] :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour? ::(a) <math>n^2</math> (b) <math>n^3</math> (c) <math>100n^2</math> (d) <math>nlogn</math> (e) <math>2^n</math> (f) <math>2^{2^n}</math> :[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>. #<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math> #<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math> #<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math> [[2.17|Solution]] :2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>. :[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>. [[2.19|Solution]] :2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math> :[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math> [[2.21|Solution]] :2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math> <center><math>(n + a)^b = \Omega (n^b)</math></center> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n & 2^n & n \lg n & \ln n \\ n-n^3+7n^5 & \lg n & \sqrt n & e^n \\ n^2+\lg n & n^2 & 2^{n-1} & \lg \lg n \\ n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1 \\ \end{array}</math> </center> [[2.23|Solution]] :2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} n^{\pi} & \pi^n & \binom{n}{5} & \sqrt{2\sqrt{n}} \\ \binom{n}{n-4} & 2^{log^4n} & n^{5(logn)^2} & n^4\binom{n}{n-4} \\ \end{array}</math> </center> :[[2.25]]. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{llll} \sum_{i=1}^n i^i & n^n & (log n)^{log n} & 2^{(log n^2)}\\ n! & 2^{log^4n} & n^{(log n)^2} & n^4 \binom{n}{n-4}\\ \end{array}</math> </center> [[2.25|Solution]] :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which. <center> <math>\begin{array}{lll} \sqrt{n} & n & 2^n \\ n \log n & n - n^3 + 7n^5 & n^2 + \log n \\ n^2 & n^3 & \log n \\ n^{\frac{1}{3}} + \log n & (\log n)^2 & n! \\ \ln n & \frac{n}{\log n} & \log \log n \\ ({1}/{3})^n & ({3}/{2})^n & 6 \\ \end{array}</math> </center> :[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.'' #<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math> #<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math> #<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math> [[2.27|Solution]] :2.28. True or False? #<math>2n^2+1=O(n^2)</math> #<math>\sqrt n= O(\log n)</math> #<math>\log n = O(\sqrt n)</math> #<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math> #<math>3n^2 + \sqrt n = O(n^2)</math> #<math>\sqrt n \log n= O(n) </math> #<math>\log n=O(n^{-1/2})</math> :[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above. #<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math> #<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math> #<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math> [[2.29|Solution]]. :2.30. For each of these questions, briefly explain your answer. ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. ::(a) Is <math>3^n = O(2^n)</math>? ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? ::(c) Is <math>3^n = \Omega(2^n)</math>? ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? [[2.31|Solution]] :2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f(n)=\sum_{i=1}^n {1\over i}</math>. #<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>. #<math>f(n)=\sum_{i=1}^n \log i</math>. #<math>f(n)=\log (n!)</math>. :[[2.33]]. Place the following functions into increasing asymptotic order. ::<math>f_1(n) = n^2\log_2n</math>, <math>f_2(n) = n(\log_2n)^2</math>, <math>f_3(n) = \sum_{i=0}^n 2^i</math>, <math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>. [[2.33|Solution]] :2.34. Which of the following are true? #<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>. #<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>. :[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>. #<math>f_1(n)= (1000)2^n + 4^n</math>. #<math>f_2(n)= n + n\log n + \sqrt n</math>. #<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>. #<math>f_4(n)= (0.99)^n + n^{100}.</math> [[2.35|Solution]] :2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of <math>B</math>. Note that zero, one or more of these relations may hold for a given pair; list all correct ones. <br><center><math> \begin{array}{lcc} & A & B \\ (a) & n^{100} & 2^n \\ (b) & (\lg n)^{12} & \sqrt{n} \\ (c) & \sqrt{n} & n^{\cos (\pi n/8)} \\ (d) & 10^n & 100^n \\ (e) & n^{\lg n} & (\lg n)^n \\ (f) & \lg{(n!)} & n \lg n \end{array} </math></center> ===Summations=== :[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0. <center> <math>\begin{array}{ccccccccc} &&&&1&&&& \\ &&&1&1&1&&&\\ &&1&2&3&2&1&&\\ &1&3&6&7&6&3&1&\\ 1&4&10&16&19&16&10&4&1\\ \end{array}</math> </center> [[2.37|Solution]] :2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.) :[[2.39]] [[2.39|Solution]] :2.40. Consider the following code fragment. <tt> for i=1 to n do for j=i to 2*i do output ''foobar'' </tt> :Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as a summation (actually two nested summations). #Simplify the summation. Show your work. :[[2.41]].Consider the following code fragment. <tt> for i=1 to n/2 do for j=i to n-i do for k=1 to j do output ''foobar'' </tt> :Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>. #Express <math>T(n)</math> as three nested summations. #Simplify the summation. Show your work. [[2.41|Solution]] :2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?) :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time. [[2.43|Solution]] ===Logartihms=== :2.44. Prove the following identities on logarithms: #Prove that <math>\log_a (xy) = \log_a x + \log_a y</math> #Prove that <math>\log_a x^y = y \log_a x</math> #Prove that <math>\log_a x = \frac{\log_b x}{\log_b a}</math> #Prove that <math>x^{\log_b y} = y^{\log_b x}</math> :[[2.45]]. Show that <math>\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1</math> [[2.45|Solution]] :2.46. Prove that that the binary representation of <math>n \geq 1</math> has <math>\lfloor \lg_2 n \rfloor</math> + <math>1</math> bits. :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in <math>O( n \log (\sqrt n) )</math>. Given the existence of an <math>\Omega(n \log n)</math> lower bound for sorting, how can this be possible? [[2.47|Solution]] ===Interview Problems=== :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? [[2.49|Solution]] :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. [[2.51|Solution]] :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? [[2.53|Solution]] :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? [[2.55|Solution]] Back to [[Chapter List]] 2f3ff358480c862a95b26df16fb714efe1700457 Main Page 0 1 460 445 2020-10-26T14:35:55Z Algowikiadmin 1 Redirected page to [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition"]] wikitext text/x-wiki #REDIRECT [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition"]] The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 8816ad00a6eb6454b550c7909b3d121eefe04d75 462 460 2020-10-26T14:37:22Z Algowikiadmin 1 Changed redirect target from [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition"]] to [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition]] wikitext text/x-wiki #REDIRECT [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition]] The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 23ca27f79dead1c96ec03469ad6cc0a171d6f2b2 464 462 2020-10-28T18:09:06Z Algowikiadmin 1 Changed redirect target from [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition]] to [[Solution Wiki, The Algorithm Design Manual, 3rd Edition]] wikitext text/x-wiki #REDIRECT [[Solution Wiki, The Algorithm Design Manual, 3rd Edition]] The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 9a73a67fd170231ce53779a2c2dd9c638eea10cc Solution Wiki, The Algorithm Design Manual, 3rd Edition 0 235 465 2020-10-28T18:09:13Z Algowikiadmin 1 Created page with " The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students..." wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) fbb16447de077f46c685617ddc342506162fd583 466 465 2020-10-28T18:14:21Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] The second edition of the solution wiki can be found [[Second Edition|here]]. == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 8f252067dbd7057d5178954ba2d526ddf88a174a 467 466 2020-10-28T18:14:32Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] The second edition of the solution wiki can be found [[Second Edition|here]]. == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) 7de34c46c47edf7545bb3aec68af687ed6e35157 468 467 2020-11-09T16:39:54Z Algowikiadmin 1 wikitext text/x-wiki The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others. Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution. <big>Chapters</big> #[[Chapter 1|Introduction to Algorithms]] #[[Chapter 2|Algorithm Analysis]] #[[Chapter 3|Data Structures]] #[[Chapter 4|Sorting]] #[[Chapter 5|Divide and Conquer]] #[[Chapter 6|Hashing and Randomized Algorithms]] #[[Chapter 7|Graph Traversal]] #[[Chapter 8|Weighted Graph Algorithms]] #[[Chapter 9|Combinatorial Search]] #[[Chapter 10|Dynamic Programming]] #[[Chapter 11|NP-Completeness]] #[[Chapter 12|Dealing with Hard Problems]] The second edition of the solution wiki can be found [https://www.algorist.com/algowiki_v2/index.php/The_Algorithms_Design_Manual_(Second_Edition) here]. == Getting started == ; Editing * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula] * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples] ; Configuration * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] ; Mediawiki General * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list] ; Questions *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems) * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic) fabc1d281c278b8be853b8f4037d5d857536b1e7